website logo

What is loop in c++ programming

Loop are iteration statement that execute set of instruction until specified or termination condition is not fulfilled . execute same statements multiple times for exit from a loop without completion we can use the break statement that jump out side the block/loop and stop executing block of code.

Loops are mainly three type:

1)For Loop

syntax of for loop
for(initial value ; test condition;increment )
{

//block of code

}
initialization condition check increment

2)While Loop

while(condition is true)
{

//block of code

}

3)Do While Loop

do
{

//block of code

}
while(condition is true);

all three loop execute a block of code until specified conditions are true or specified number of iteration are not completed
For loop:

For loop are loop that execute block of code till specified conditions are true. in for loop we initialize variable, check condition and increment or decrement variable in same line or loop.
While loop:

While loop are approx same as for loop. while loop initialize variable before loop and increment or decrement variable in block of code.
Do-while loop:
Do while loop are almost same as while only difference is do while loop execute at least once without checking condition means block of code specify in loop are execute first after condition are checked by loop.


Example of loop

Print ten number using loop

For loop:

#include<iostrem>
int main()
{
for(int i=1;i<11;i++)
{
cout <<"number "<<i;
}
}
While loop:

#include<iostrem>
int main()
{
int i=1;
while(i<11)
{
cout <<"number "<<i;
i++;
}
}
Do-while loop:
#include<iostrem>
int main()
{
int i=1;
do
{
cout <<"number "<<i;
i++;
}while(i<11);
}