Skip to content

Loops

Loops are used to execute a block of code multiple times. They are useful when you want to repeat a task multiple times without writing the same code over and over again. In C++, there are four types of loops:

for Loop

The for loop is used to execute a block of code a specified number of times. Here is the syntax of the for loop:

for (initialization; condition; update) {
// code to be executed
}
  • The initialization statement is executed only once at the beginning of the loop.
  • The condition statement is evaluated before each iteration of the loop. If it is true, the loop continues. If it is false, the loop terminates.
  • The update statement is executed at the end of each iteration of the loop.

Here is an example of using the for loop to print the numbers from 1 to 5:

for (int i = 1; i <= 5; i++) {
cout << i << endl;
}

and the output will be:

Terminal window
1
2
3
4
5

while Loop

The while loop is used to execute a block of code as long as the condition is true. Here is the syntax of the while loop:

while (condition) {
// code to be executed
}

The condition statement is evaluated before each iteration of the loop. If it is true, the loop continues. If it is false, the loop terminates.

Here is an example of using the while loop to print the numbers from 1 to 5:

int i = 1;
while (i <= 5) {
cout << i << endl;
i++;
}

and the output will be:

Terminal window
1
2
3
4
5

do-while Loop

The do-while loop is similar to the while loop, but the condition is evaluated after the block of code is executed. This means that the block of code is executed at least once. Here is the syntax of the do-while loop:

do {
// code to be executed
} while (condition);

The condition statement is evaluated after each iteration of the loop. If it is true, the loop continues. If it is false, the loop terminates.

Here is an example of using the do-while loop to print until the number is less than or equal to 5:

int i = 1;
do {
cout << i << endl;
i++;
} while (i <= 5);

and the output will be:

Terminal window
1
2
3
4
5

Nested Loops

Nested loops are loops within loops. They are useful when you want to repeat a task multiple times, and each repetition of the task requires another repetition. Here is an example of using nested loops to print a pattern:

for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}

and the output will be:

Terminal window
*
* *
* * *
* * * *
* * * * *

break and continue Statements

The break statement is used to exit a loop. It is often used to exit a loop when a certain condition is met. Here is an example of using the break statement:

for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
cout << i << endl;
}

and the output will be:

Terminal window
1
2

The continue statement is used to skip the rest of the code inside a loop and continue with the next iteration. Here is an example of using the continue statement:

for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << endl;
}

and the output will be:

Terminal window
1
2
4
5