Updated March 17, 2023
Introduction on Keywords in C++
Keywords are the reserved keywords that are defined by the compiler to perform the internal operation, written in lowercase. Keywords have some meaning which is defined by the compiler to accomplish a task in code, they cannot be used as a variable in programming. C++ provides 64 keywords – for, break, continue, switch, int float, double, char, try, catch, while, etc.
List of C++ Keywords
Below is the list of keywords used in the C++ language.
(Auto, double, int, struct, Break, else, long, switch, Case, enum, register, typedef, Char, extern, return, union, Const, float, short, unsigned, Continue, for, signed, void, Default, goto, sizeof, volatile, Do, if, static, while, Asm, dynamic_cast, namespace, reinterpret_cast, Bool, explicit, new, static_cast, Catch false, operator, template, Class, friend, private, this, Const_cast, inline, public, throw, Delete, mutable, protected, true, Try, typeid, typename, using, virtual, wchar_t)
Example of Keywords
In the above section, we have seen the list of keywords. We cannot use them as variables in programming. In this section, we are going to discuss some of them with the help of some examples.
1. If
This keyword is used to check the condition. If the condition becomes true, it executes the statement following if.
Program for implantation of If keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number:" << endl;
cin >> n;
if(n > 0)
{
cout << "You have entered positive number";
}
return 0;
}
Output will be as follows:
2. else
This keyword is used with if statement. If the condition becomes false, the statement following the else will be executed.
Program for implantation of else keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number:" << endl;
cin >> n;
if(n %2 == 0)
{
cout << "You have entered even number";
}
else
{
cout << "You have entered odd number";
}
return 0;
}
Output will be as follows:
3. switch
This keyword is used to test the value of the variable and compare it with different cases. Based on the cases, it executes the statement. In the first output, the entered number is among the cases, so it executes the statement flowing the match cases. In the second output, we can see that, the entered number does not match the cases, hence it executes the statement follows the default keyword.
Program for implantation of switch keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number between 1 to 3" << endl;
cin >> n;
switch(n)
{
case 1:
cout << "You have entered 1";
break;
case 2:
cout << "You have entered 2";
break;
case 3:
cout << "You have entered 3";
break;
default:
cout << "Please enter valid number";
break;
}
return 0;
}
Output will be as follows:
4. while
This keyword is used to control the flow of execution. First, it checks the condition, if condition matches, statements the following while will be executed.
Program for implantation of while keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n, i = 0;
cout << "Enter a number to print the number series" << endl;
cin >> n;
cout << "List of numbers:" << endl;
while(i <= n)
{
cout << i << endl;
i++;
}
return 0;
}
Output will be as follows:
5. do
The do keyword is used with a keyword while. Working of do is the same, the only difference does first execute the statement and then check the condition, on the other hand, the whole first check condition then executes the statements.
Program for implantation of do keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n, i = 0;
cout << "Enter a number to print the number series" << endl;
cin >> n;
cout << "List of numbers:" << endl;
do
{
cout << i << endl;
i++;
}while(i <= n);
return 0;
}
Output will be as follows:
6. for
The for keyword is used to control the loop. It is a loop control statement that executes the loop until the given condition matches.
Program for implantation of for keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter a number to print the number series" << endl;
cin >> n;
cout << "List of numbers:" << endl;
for(i = 0; i <= n; i++)
{
cout << i << endl;
}
return 0;
}
Output will be as follows:
7. break
This keyword is to used to break the statement. Based on the condition, it breaks the loop. A keyword break can also be used with a switch statement to break the loop.
Program for implantation of break keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter a number to print the number series" << endl;
cin >> n;
cout << "List of numbers" <<endl;
for(i = 0; i <= n; i++)
{
if(i == 5)
{
break;
}
cout << i << endl;
}
return 0;
}
Output will be as follows:
8. continue
The continue keyword is to used to skip the line. It is exactly the opposite of the break statement. Continue Statement will continue the execution of the loop, it only skips the line where it is mentioned.
Program for implantation of continue keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter a number to print the number series" << endl;
cin >> n;
cout << "List of numbers" <<endl;
for(i = 0; i <= n; i++)
{
if(i == 5)
{
continue;
}
cout << i << endl;
}
return 0;
}
Output will be as follows:
9. goto
The goto keyword is used to transfer the flow of control to another label. In the program given below, we create two labels even and odd to transfer the flow of control.
Program for implantation of goto keyword is given as follows:
#include<iostream>
using namespace std;
void checkevenodd()
{
int n;
cout << "Enter number:" << endl;
cin >> n;
if(n %2 == 0)
goto even;
else
goto odd;
even:
cout << "You have entered even number";
return;
odd:
cout << "You have entered odd number";
}
int main()
{
checkevenodd();
return 0;
}
Output will be as follows:
10. const
This keyword is used to fix the value of the variable. A variable that is declared as const remains constant. The value of the constant variable cannot be changed. In the first program, you can see that the value of i has been changed. In the second program when we are going to change the value of constant, be it throws the error.
Program for implantation of const keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int i = 7;
i = 5;
cout << i;
return 0;
}
Output will be as follows:
Here is another program for implantation of else keyword is as follows
#include<iostream>
using namespace std;
int main()
{
const int i = 7;
i = 5;
cout << i;
return 0;
}
Output will be as follows:
11. struct
This keyword is used to create a data type. It declares the all the variables in one group be following the struct keyword. After that creating an object of the struct, all the variables are retrieved.
Program for implantation of struct keyword is given as follows:
#include<iostream>
using namespace std;
struct student
{
string first_name, last_name;
int roll_no;
};
int main()
{
struct student stu;
stu.first_name = "EDU";
stu.last_name = "CBA";
stu.roll_no = 5;
cout << "First Name:" << stu.first_name << endl;
cout << "Last Name:" << stu.last_name << endl;
cout << "Roll No:" << stu.roll_no << endl;
return 0;
}
Output will be as follows:
12. char
This keyword is used to declare a character variable. For example, char spelling; So here, spelling is a character type variable.
Program for implantation of char keyword is given as follows:
#include <iostream>
using namespace std;
int main()
{
char alphabet[100];
cout << "Enter a string: ";
cin >> alphabet ;
cout << "You entered: " << alphabet << endl;
cout << "\nEnter another alphabet: ";
cin >> alphabet;
cout << "You entered: "<<alphabet<<endl;
return 0;
}
Output will be as follows:
13. int
This keyword is used to store the integer values. You can see that, even after assigning float value, it gives the only integer as an output.
Program for implantation of int keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
int a = 3.14;
cout << a;
return 0;
}
Output will be as follows:
14. float
This keyword is used to store decimal values.
Program for implantation of float keyword is given as follows:
#include<iostream>
using namespace std;
int main()
{
float a = 3.14;
cout << a;
return 0;
}
Output will be as follows:
15. class
This keyword is used to create a class. A class contains the data and the function which are going to be used in the program. A class can be governed by three keywords – public, protected and private. The member and function of the class can be accessed outside the class. Members and function of a private class cannot be accessed outside the class.
Program for implantation of class keyword is given as follows:
#include<iostream>
using namespace std;
class student
{
public:
string first_name, last_name;
int roll_no;
};
int main()
{
student stu;
stu.first_name = "EDU";
stu.last_name = "CBA";
stu.roll_no = 5;
cout << "First Name:" << stu.first_name << endl;
cout << "Last Name:" << stu.last_name << endl;
cout << "Roll No:" << stu.roll_no << endl;
return 0;
}
Output will be as follows:
Conclusion
In this article, we have seen what are the various keywords in C++ and how to implement these keywords using some examples. I hope you will find this article helpful.
Recommended Articles
This has been a guide to C++ Keywords. Here we have discussed what is C++ Keywords, List of C++ keywords along with Examples following with codes and outputs. You can also go through our other suggested articles to learn more–