What is Loops in c++?
A loop statement allows us to execute a statement or group of statements multiple times and the following is the general form of a loop statement in most programming languages −
Two types of Loops?
- Entry Control loop
- Exit Control loop

1) Entry Control loop (Pre Test Loop)
The control conditions are tested before the start of the loop execution. If the conditions are not satisfied, then the body of the loop will not be executed.
There are two types of Entry Control loops:
- For loop
- While loop
2) Exit Control loop (Post Test Loop)
The Test is performed at the end of the body of the loop and therefore the body is executed unconditionally for the first time.
There is one type of Entry Control loop :
- do While loop
Also Read This :- variable in c++
While Loop in c++
A while loop statement repeatedly executes a target statement as long as a given condition is true.
The syntax of a while loop is :
while (condition) { statement(s); }
Here is the flow of control in a while loop:
Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Here the key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example: display the sum of the first 10 natural numbers using a while loop.
#include<iostream> #include<conio.h> using namespace std; main(){ Â Â int i = 1, sum = 0; Â Â while(i <= 10) Â Â { Â Â Â Â sum = sum + i; Â Â Â Â i++; Â Â } Â Â cout<<"Sum = "<<sum; Â Â getch(); Â Â return(0); }
output
Sum = 55
For Loop in c++
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
The syntax of a for loop is :
for (initialization; condition; increment/decrement) { statement(s); }
Here is the flow of control in a for loop:
The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the for a loop.
After the body of the for loop executes, the flow of control jumps back up to the increment /decrement statement. This statement allows you to update any loop control variables.
The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of the loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
Example: display the sum of the first 10 natural numbers using for loop.
#include<iostream> #include<conio.h> using namespace std; main(){ Â Â int i = 1, sum = 0; Â Â for(i = 1; i <= 10; i++) Â Â { Â Â Â Â sum = sum + i; Â Â } Â Â cout<<"Sum = "<<sum; Â Â getch(); Â Â return(0); }
output
Sum = 55
Do while Loop in c++
Unlike for and while loops, which test the loop condition at the top of the loop, the do…while loop in C programming language checks its condition at the bottom of the loop.
A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.
The syntax of a do..while loop is :
do { statement(s); }while( condition );
Here is the flow of control in a do-while loop:
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
Example: display the sum of the first 10 natural numbers using a do-while loop.
#include<iostream> #include<conio.h> using namespace std; main(){   int i = 1, sum = 0;   do   {     sum = sum + i;     i++;   }while(i <= 10);   cout<<"Sum = "<<sum;   getch();   return(0); }
output
Sum = 55
Nesting of a loop in c++
We can also use loops within loops.
i.e. one for statement within another for statement is allowed in C. (or “C‟ allows multiple for loops in the nested forms).
The syntax of nesting of the loop is :
for( initialization ; test condition ; increment/decrement) /* outer loop */ { for(initialization ; test condition ; increment/decrement) /* inner loop */ { Body of loop; } }
Example :
#include<iostream> #include<conio.h> using namespace std; main(){ Â Â int i,j,k; Â Â k = 1; Â Â for(i = 1; i <= 5; i++) Â Â { Â Â Â Â for(j = 1; j <= i; j++) Â Â Â Â { Â Â Â Â Â Â cout<< k << " "; Â Â Â Â Â Â k++; Â Â Â Â } Â Â Â Â cout<<"\n"; Â Â } Â Â getch(); Â Â return(0); }
output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15