C++ Keywords Reference šŸŽÆ

beginner
16 min

C++ Keywords Reference šŸŽÆ

Welcome to our deep dive into the world of C++! In this comprehensive guide, we'll explore various C++ keywords that are essential for every programmer. Whether you're a beginner or an intermediate learner, this guide will help you understand these keywords from the ground up. Let's get started!

Basic Keywords šŸ“

Here are some basic C++ keywords that you'll encounter frequently:

  1. int: Used to declare an integer variable.
cpp
int myNumber = 10;
  1. float: Used to declare a floating-point number variable.
cpp
float myFloat = 10.5;
  1. double: Used to declare a double-precision floating-point number variable.
cpp
double myDouble = 10.5;
  1. char: Used to declare a character variable.
cpp
char myCharacter = 'A';
  1. bool: Used to declare a boolean variable.
cpp
bool isTrue = true;
  1. void: Used to declare functions that don't return a value.
cpp
void printMessage() { std::cout << "Hello, World!"; }

Control Structures Keywords šŸ“

Control structures are used to control the flow of your program. Here are some important keywords:

  1. if: Used to execute a block of code if a certain condition is true.
cpp
if (myNumber > 10) { std::cout << "myNumber is greater than 10."; }
  1. else: Used to execute a block of code if the if condition is false.
cpp
if (myNumber > 10) { std::cout << "myNumber is greater than 10."; } else { std::cout << "myNumber is not greater than 10."; }
  1. else if: Used to test multiple conditions.
cpp
if (myNumber > 10) { std::cout << "myNumber is greater than 10."; } else if (myNumber < 5) { std::cout << "myNumber is less than 5."; } else { std::cout << "myNumber is between 5 and 10."; }
  1. switch: Used to execute different blocks of code based on the value of an integer expression.
cpp
switch (myNumber) { case 10: std::cout << "myNumber is 10."; break; case 5: std::cout << "myNumber is 5."; break; default: std::cout << "myNumber is neither 5 nor 10."; }
  1. for: Used to repeat a block of code a specific number of times.
cpp
for (int i = 0; i < 10; i++) { std::cout << i << " "; }
  1. while: Used to repeat a block of code as long as a certain condition is true.
cpp
int i = 0; while (i < 10) { std::cout << i << " "; i++; }
  1. do...while: Used to repeat a block of code at least once, as long as a certain condition is true.
cpp
int i = 0; do { std::cout << i << " "; i++; } while (i < 10);

Quiz šŸ’”

Quick Quiz
Question 1 of 1

What is the purpose of the `if` keyword in C++?

Stay tuned for more! In the next part, we'll delve into more advanced C++ keywords and concepts. Happy coding! šŸ’”