C++ Keywords
Reserved words that have special meaning in C++
🔑 What are C++ Keywords?
Keywords are reserved words in C++ that have predefined meanings. You cannot use them as variable names, function names, or identifiers. They form the vocabulary of C++ programming language.
// Keywords in action
int main() { // 'int' and 'main' are keywords
if (true) { // 'if' and 'true' are keywords
return 0; // 'return' is a keyword
}
}
Keyword Categories
Data Types
Keywords for defining data types
int number = 10;
double price = 99.99;
char letter = 'A';
bool flag = true;
Control Flow
Keywords for program flow control
if (condition) {
// do something
} else {
// do something else
}
Loops
Keywords for repetitive operations
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
Classes
Keywords for object-oriented programming
class MyClass {
public:
void method() { }
};
🔹 Common Data Type Keywords
Data type keywords define the nature and storage requirements of information in a program, forming the
foundation of variable declaration. Primitive types like int, float,
double, char, and bool represent integers, real numbers, characters, and
Boolean values. Type modifiers (short, long, signed, unsigned)
adjust size and sign, while void indicates the absence of a type. Choosing the correct type is critical
for memory efficiency, preventing overflow errors, ensuring mathematical accuracy, and writing clear, intentional
code that correctly models the problem domain.
#include <iostream>
using namespace std;
int main() {
// Integer types
int age = 25;
short smallNum = 100;
long bigNum = 1000000;
// Floating point types
float temperature = 36.5f;
double precision = 3.14159;
// Character and boolean
char initial = 'J';
bool isActive = true;
cout << "Age: " << age << endl;
cout << "Temperature: " << temperature << endl;
cout << "Active: " << isActive << endl;
return 0;
}
Output:
Age: 25
Temperature: 36.5
Active: 1
🔹 Control Flow Keywords
Control flow keywords dictate the order in which instructions are executed, enabling programs to make
decisions and branch based on dynamic conditions. The if-else construct
handles binary or multi-way choices. The switch statement provides a cleaner alternative for selecting
one option from many based on a single integral value. Keywords like break (to exit a switch case or
loop) and continue (to skip to the next loop iteration) provide finer control within these structures.
Mastering these is key to implementing game logic, user interface state machines, and business rule validation.
#include <iostream>
using namespace std;
int main() {
int score = 85;
// if-else statement
if (score >= 90) {
cout << "Grade A" << endl;
} else if (score >= 80) {
cout << "Grade B" << endl;
} else {
cout << "Grade C" << endl;
}
// switch statement
char grade = 'B';
switch (grade) {
case 'A':
cout << "Excellent!" << endl;
break;
case 'B':
cout << "Good job!" << endl;
break;
default:
cout << "Keep trying!" << endl;
}
return 0;
}
Output:
Grade B
Good job!
🔹 Loop Keywords
Loop constructs automate repetition, allowing code to execute multiple times, which is fundamental for
processing collections and iterative algorithms. The for loop, with its compact
initialization-condition-increment syntax, is ideal for counted iterations. The while loop repeats
based on a pre-checked condition, suitable for reading data until a sentinel value appears. The
do...while loop guarantees one execution before checking its condition, perfect for menus or input
validation. These are essential for tasks like traversing arrays, calculating sums, simulating processes, and
rendering frames in an application.
#include <iostream>
using namespace std;
int main() {
cout << "For loop: ";
// for loop
for (int i = 1; i <= 3; i++) {
cout << i << " ";
}
cout << endl;
cout << "While loop: ";
// while loop
int j = 1;
while (j <= 3) {
cout << j << " ";
j++;
}
cout << endl;
cout << "Do-while loop: ";
// do-while loop
int k = 1;
do {
cout << k << " ";
k++;
} while (k <= 3);
return 0;
}
Output:
For loop: 1 2 3
While loop: 1 2 3
Do-while loop: 1 2 3
🔹 Complete Keywords List
This categorized list provides a structured overview of all C++ keywords, essential for understanding the
language's syntax and capabilities. Beyond the basic types and control flow, it includes
function-related keywords (return, inline), object-oriented keywords (class,
virtual), and memory management operators (new, delete). Familiarity with the
full lexicon helps in reading advanced code, understanding compiler errors, and utilizing modern C++ features
effectively. This comprehensive knowledge is what separates novice programmers from proficient developers who can
leverage the language's full power.
Data Types:
int, float, double, char, bool, short, long, signed, unsigned, void
Control Flow:
if, else, switch, case, default, break, continue, goto
Loops:
for, while, do
Functions:
return, inline, extern
Classes & Objects:
class, struct, public, private, protected, virtual, friend
Memory:
new, delete, sizeof, const, static, auto