Loop in C++


Loop in C++ refers to the execution of a statement or block of statements repeatedly if the condition is true. Looping makes the code shorter and easier as we don’t need to write the same statements again and again. Usually, a loop structure consists of the following parts:

  • Initialization
  • Check condition
  • Repeat the block of statements
  • Stop execution of the loop when condition becomes false

It can be shown in figure as below:

Loop in C++

Types of Loop in C++

Like Loop in C, in C++ also, there are 3 types of loop structures available. These are –

While loop

while loop is an entry controlled loop because the condition is tested before entering into the loop. When, the number of times of executing a loop is not known, in such cases while loop is used. It mainly has 3 steps as follows:

  • initialization
  • test condition
  • variable increment/decrement

It has the syntax as follows:

First, the variable is initialized with a value. Then, this variable is tested to follow a certain condition. If the condition is true, block of statement is executed and the variable is increment or decrement. If the condition is false, the loop is not executed. The loop is executed till the condition is true. For example:

The output of the loop will be:

Hence, while loop will be executed 6 times. When, the value of ‘a’ become 7, the control will be come out of the loop and next statement is executed.

Do-while loop

do-while loop is an exit controlled loop because the condition is tested after execution of the loop. First, the do statement evaluates the body of the loop and then while statement evaluates the condition. It mainly has 4 steps as follows:

  • initialization
  • execution of loop body
  • variable increment or decrement
  • check condition

It has the syntax as follows:

First, the variable is initialized with a value. Then, the block of statement is executed and the variable is increment or decrement. After that condition is checked in the while statement. If the condition is true, then the loop is executed again, otherwise control leaves the loop block and executes the next statement. In the do-while loop, even if the condition is false, the loop is executed at least once, because the condition is checked at the end of the loop.
For example:

The output of the loop will be:

Hence, do-while loop will be executed 6 times. When, the value of ‘a’ become 7, the control will be come out of the loop and next statement is executed.



For loop

for loop is an entry controlled loop because the condition is tested before entering into the loop. For loop is used, when we know the exact number of how many times looping is to done. It has the syntax as follows:

In the for loop expression, 1st the variable is initialized with a value, 2nd, the condition is checked in response to the variable and 3rd the variable is increment or decrement after each execution of the loop. For example, if we want to print the first 10 natural number, then we can use for loop as follows:

The output of the above loop is:

Nested loop

for loop can be nested to any numbers which means we can use as many for loops inside another for loop as we want. It works on Last In First Out manner, which means inner loop executed before the outer loop. It has the syntax as follows:

For example:

It will give the output as:

Loop Control Statement in C++

Loop control in C++ statement changes the normal sequence of executing a loop. When we use loop control statement, we can transfer the control to the different statement from the loop block. There are mainly 3 types of loop control statements. These are:

  • goto statement: goto statement transfer the control to the labeled statement from the loop.
  • break statement: when a break statement is encountered inside the loop, then the control automatically leaves the loop and go to the next statement immediately after the loop.
  • continue statement: when a continue statement is encountered, control leaves the current sequence and executes the next sequence.
Close Menu