C++ Keywords

When programming in C++, it is essential to understand the various keywords that form the foundation of the language. Keywords are reserved words that have specific meanings and functionalities within the C++ programming language. In this article, we will explore some of the most commonly used C++ keywords and provide examples to illustrate their usage.

1. int

The int keyword is used to declare variables of type integer. It is one of the fundamental data types in C++. Here’s an example:

    int age = 25;

In this example, we declare a variable called “age” and assign it the value of 25.

2. double

The double keyword is used to declare variables of type floating-point. It represents real numbers with decimal places. Here’s an example:

    double pi = 3.14159;

In this example, we declare a variable called “pi” and assign it the value of 3.14159.

3. bool

The bool keyword is used to declare variables of type boolean. It represents either true or false values. Here’s an example:

    bool isRaining = true;

In this example, we declare a variable called “isRaining” and assign it the value of true, indicating that it is currently raining.

4. if

The if keyword is used to create conditional statements. It allows us to execute a block of code only if a certain condition is true. Here’s an example:

    int number = 10;
if (number > 5) {
    cout << "The number is greater than 5";
}

In this example, we check if the variable “number” is greater than 5. If the condition is true, the message “The number is greater than 5” will be displayed.

5. for

The for keyword is used to create a loop that executes a block of code a specific number of times. It is often used when we know the exact number of iterations required. Here’s an example:

    for (int i = 0; i < 5; i++) {
    cout << i << " ";
}

In this example, the loop will iterate five times, and the variable “i” will take on the values 0, 1, 2, 3, and 4. The output will be: 0 1 2 3 4.

6. while

The while keyword is used to create a loop that executes a block of code as long as a specified condition is true. Here’s an example:

    int count = 0;
while (count < 5) {
    cout << count << " ";
    count++;
}

In this example, the loop will iterate as long as the variable “count” is less than 5. The output will be: 0 1 2 3 4.

7. class

The class keyword is used to define a user-defined data type in C++. It serves as a blueprint for creating objects. Here’s an example:

    class Rectangle {
public:
    int width;
    int height;
};

Rectangle myRectangle;

In this example, we define a class called “Rectangle” with two member variables: “width” and “height”. We then create an object of type “Rectangle” called “myRectangle”.

8. return

The return keyword is used to exit a function and return a value. It is often used to provide the result of a function back to the caller. Here’s an example:

    int add(int a, int b) {
    return a + b;
}

In this example, the function “add” takes two integer parameters and returns their sum using the return keyword.

These are just a few examples of the many keywords available in C++. Understanding and correctly using these keywords is crucial for writing effective and efficient C++ programs. By mastering these keywords, you will be well on your way to becoming a proficient C++ programmer.

Scroll to Top