C++ Loop Control Statements

C++ Loops are used to execute the same block of code a specified number of times or while a specified condition is true.

Very often when we write code, we want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a code we can use loops to perform a repetitive task like this.

In C++ there are mainly three different kinds of loops:

  • while loop – loops through a block of code while a specified condition is true.
  • do-while loop – a variant of the while loop which will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested.
  • for loop loops through a block of code a specified number of times

 

1) The while loop

The while loop is used when we want the loop to execute and continue executing while the specified condition is true. As the condition is checked at the beginning of the loop, it is also called entry controlled loop.

Syntax

while (condition) {
//code to be executed
}
 
Example 1.

See the program below.

loop01.cpp

#include <iostream>
using namespace std;

int main() {
int i = 1;

//while loop
while (i <= 5) {
cout << "The number is " << i << endl;
i = i + 1;
}
return 0;
}

Output

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

 

 

2) The do-while Loop

The do-while loop is a variant of the while loop. This loop will always execute a block of code at least once, and then it will repeat the loop as long as the specified condition is true. This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested. As the condition is checked at the end of the loop, it is also called exit controlled loop

Syntax

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

See the program below.

loop02.cpp

#include <iostream>
using namespace std;

int main() {
int i = 1;

//do-while loop
do {
cout << "The number is " << i << endl;
i = i + 1;
} while (i <= 5);

return 0;
}

Output

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

 

 

3) The for Loop

The for loop is used when we know in advance how many times the code should run.

Syntax

for (initialization part; condition part; re-initialization part) 
{   
   //code to be executed
}
 
Example 3.

The example below defines a loop that starts with i=1. The loop will continue to run as long as i is less than, or equal to 5. The value of i will increase by 1 each time the loop runs.

loopl03.cpp

#include <iostream>
using namespace std;

int main() {
int i;

//for loop
for (i = 1; i <= 5; i++) {
cout << "The number is " << i << endl;
}
return 0;
}

Output

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

 

 

♦ C++ break and continue statements

There are two special statements that can be used inside loops: break and continue. See examples below.

 

 Using “break” statement:

The break statement will break the loop and continue executing the code that follows after the loop (if any).

Example 4.

See the following example.

loop04.cpp

#include <iostream>
using namespace std;

int main() {
int i;

//break statement inside loop
for (i = 1; i <= 5; i++) {
if(i==3)
break;
cout << "The number is " << i << endl;
}
return 0;
}

Output

The number is 1
The number is 2
 

 

Using “continue” statement :

The continue statement ​will break the current loop and continue with the next value.

Example 5.

See the example below.

loop05.cpp

#include <iostream>
using namespace std;

int main() {
int i;

//continue statement inside loop
for (i = 1; i <= 5; i++) {
if(i==3)
continue;
cout << "The number is " << i << endl;
}
return 0;
}

Output

The number is 1
The number is 2​
The number is 4
The number is 5​​