C++ For Loop Examples

Real Life Example

To demonstrate a practical example of the for loop, let’s create a program that counts to 100 by tens:

Example

for (int i = 0; i <= 100; i += 10) {
  cout << i << "\n";
}

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

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++ 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 For Loop Examples

Real-Life Examples

To demonstrate a practical example of the for loop, let’s create a program that counts to 100 by tens:

Example

for (i = 0; i <= 100; i += 10) {
  printf("%d\n", i);
}

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

C While Loop Examples

Real-Life Examples

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

Example

int countdown = 3;

while (countdown > 0) {
  printf("%d\n", countdown);
  countdown--;
}

printf("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