In this tutorial, you will learn about CPP keywords. We have a list of C++ keywords with their examples. How to use keywords in C++ language and why you need to use them.
Introduction Keywords in C++
The keyword is a word in C+ programming language which has a predefined meaning and, purpose. The purpose of the keywords is fixed by the developer of the language. It can’t be modified by the user. The keyword can be used for the same purpose for which it is defined. Keywords are also known as reserved words. There are many types of keywords that are used in c++ language. C++ provides more than 60 keywords that we can use in c++ programs.
Keywords are certain words whose meaning is already explained by Compiler. Those keywords can not be used as an identifier in c++ language. Note that keywords are the set of reserved words and predefined identifiers. Predefined identifiers are identifiers that are specified by the compiler but can be modified in meaning by the user. So, the following keywords are used in C++.
C++ Keywords | Keywords | C++ Keywords | Keywords |
---|---|---|---|
auto | asm | bool | break |
case | catch | char | class |
const | cont-cast | continue | default |
delete | do | double | dynamic-cast |
else | enum | explicit | export |
extern | false | float | for |
friend | goto | if | inline |
int | long | mutable | namespace |
new | operator | private | protected |
public | register | reinterpret_cast | return |
short | signed | sizeof | static |
static_cast | struct | switch | template |
this | throw | true | try |
typedef | typeid | typename | union |
unsigned | using | virtual | void |
volatile | wchar-t | while |
Why we use c++ keywords
We use Keywords in C++ because it helps the user in composing statements and commands in a language. Each keyword sends a unique meaning to the compiler to perform a specific task. As we require to use usual grammar to form a meaningful sentence, we require to be strong-acquainted with the syntax of C++ to tell the compiler what to do.
Now let’s take some examples of C++ Keywords.
do keyword: The do keyword is used with a while keyword. Working on this keyword is the same, the only difference does first perform the statement and then compare the condition, on the other hand, the first complete check condition then executes the statements.
#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; }
while Keyword: This keyword is used to check the progress of execution. First, it marks the condition, if the condition meets, statements the following while will be executed.
#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; }
break Keyword: It is used to break the statement. Based on the position, it breaks the loop. A break keyword can be used with a switch statement to break the loop.
#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; }
If Keyword: This keyword is used to control the condition. If the condition becomes true, it performs the statement following if.
#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; }
else Keyword: The keyword is used with if statement. If the condition becomes false, the statement following the else will be performed.
#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; }