C++ For Loop

C++ For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed. Continue reading C++ For Loop

C++ While Loop Examples

Real Life Example

To demonstrate a practical example of the while loop, we have created a simple “countdown” program:

Example

int countdown = 3;

while (countdown > 0) {
  cout << countdown << "\n";
  countdown--;
}

cout << "Happy New Year!!\n";

In this example, we create a program that only print even numbers between 0 and 10 (inclusive): Continue reading C++ While Loop Examples

C++ Do/While Loop

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Continue reading C++ Do/While Loop

C++ While Loop

C++ Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.


Continue reading C++ While Loop

C++ Switch

C++ Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break and default keywords are optional, and will be described later in this chapter

The example below uses the weekday number to calculate the weekday name: Continue reading C++ Switch

C++ If … Else Examples

Real Life Example

This example shows how you can use if..else to “open a door” if the user enters the correct code :

Example

int doorCode = 1337;

if (doorCode == 1337) {
  cout << "Correct code.\nThe door is now open.\n";
} else {
  cout << "Wrong code.\nThe door remains closed.\n";
}

This example shows how you can use if..else to find out if a number is positive or negative : Continue reading C++ If … Else Examples

C++ Short Hand If Else

Short Hand If…Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.

It can be used to replace multiple lines of code with a single line, and is often used to replace simple if else statements: Continue reading C++ Short Hand If Else

C++ Else If

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

Continue reading C++ Else If

C++ Else

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

Example

int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

Continue reading C++ Else

C++ If … Else

C++ Conditions and If Statements

You already know that C++ supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Continue reading C++ If … Else