
C++ Loops
There may be a situation when we need to execute a block of the program several times. Therefore, In a programming language, loops are used when we need to execute a block of statements repeatedly.
The following is the general from of a loop statement:

There are mainly two types of Loops:

- Entry Controlled loops: The test condition is tested before entering the loop body. While Loop and For Loop is entry-controlled loops.
- Exit Controlled Loops: The test condition is tested at the end of the loop body. Hence, the loop body will execute at least once, irrespective of whether the test condition is true or false. The do-while loop is exit controlled loop.
Concept of Loop statements
Sr.No | Loop Type & Description |
---|---|
1 | while loop– Repeats a statement or group of statements while a given condition is true. It evaluates the condition before executing the loop body. |
2 | for loop– Execute a sequence of statements multiple times and reduces the code that manages the loop variable. |
3 | do…while– Just like a while statement, only difference is that it tests the condition at the end of the loop body. |
4 | nested loops– A loop inside another loop is called a nested loop. |
C++ While Loop
Syntax:
while (condition)
{
// body of the loop
}
- A
while
loop assess thecondition
- If the
condition
assessment istrue
, the code inside thewhile
loop is executed. - Then the
condition
is assessed again. - This process continues until the
condition
assessment isfalse
. - When the
condition
assessment isfalse
, the loop terminates.

C++ for LOOP
for (initialization; condition; update)
{
// body of-loop
}
Here,
initialization
– Variable is initialized and is executed only oncecondition
– If the assessment of the condition istrue
, the body offor
loop is executed and
iffalse
, the for loop gets terminatedupdate
– Updates the value of initialized variables and checks the condition again

C++ do…while loop
Here,
- At first, the body of the loop is executed. Then the condition is evaluated.
- If the evaluation of the condition is true, the body of the loop inside the do statement is executed again.
- The
condition
is evaluated again. - If the
condition
assessment istrue
, the body of the loop inside thedo
statement is executed again. - This process keeps repeating until the
condition
evaluates tofalse
. Then the loop terminates.

Loop Control Statements
Loop control statements change the execution of the program from their normal sequence.
Following are the control statements in C++:
Sr.No | Control Statement & Description |
---|---|
1 | break statement– Terminates the loop and transfers execution to the statement immediately following the loop. Also used in switch statements. |
2 | continue statement– Causes the loop to skip the remaining body and immediately retest its condition prior to reiterating. |
3 | goto statement– Transfers control to the labeled statement. |
The Infinite Loop
A loop becomes an infinite loop if the evaluation of the condition is never false.
Example:
#include <iostream>
using namespace std;
int main(){
for(int i=1; i>=1; i++){
cout<<"GeeksGod"<<endl;
}
return 0;
}
OUTPUT
------
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
...
Also,
#include <stdio.h>
int main()
{
for(;;) // Absent Condition
{
printf("GeeksGod\n");
}
return 0;
}
When the conditional expression is absent, it is assumed to be true.