Yesterday i completed my exercises on loop , that's why today i've decided to create a tutorial on loop in c++.
Loop
loop allows us to repeat a statement or a group of statements. When we want to repeat a single statement then it is not necessary to put curly braces But if we want to repeat a group of statements then it must be enclosed with curly braces. And it is a good practise to enclosed your loop body with curly braces.
c++ has three loop control structures
while loop
do-while loop
for loop
while loop
Structure of while loop is as follow
while(condition)
{
statement;
statement;
statement;
}
while loop is entry controlled loop because it first check the condition And if result is found True then statements get executed.
let's see example
int a=1;
while(a<=10)
{
cout<<a++;
cout<<endl
}
Output:
1
2
3
4
5
6
7
8
9
10
do-while loop
do-while loop is exit controlled loop because condition is evaluated after each iteration.
i=0;
do
{
cout<<++i<<"\t";
}while(i<10);
Output:-
1 2 3 4 5 6 7 8 9 10
do-while loop will execute a statement or a group of statement for atleast one time even the condition is Zero(false).
let's see example
do
{
cout<<"Hello";
}while(0);
Output:
Hello
For Loop
Syntax of for loop(syntax may be different based on the requirement , here i put what is normally used):
for(initialization;condition;increment/decrement)
{
statement;
statement;
statement;
}
Once initialization is done , it check the condition , and if condition is found true then statements get executed. Then increment/decrement part gets executed.Again it check the condition and execute statements and then increment/decrement. This process continues until condition is false.
let's see example:
for(int i=1;i<=10;i++)
{
cout<<2*i<<"\t";
}
Output:
2 4 6 8 10 12 14 16 18 20
It is not necessary that the last part of the loop must be increment / decrement.
let's see example:
for(int i=0;i<10;cout<<"\t")
{
cout<<++i;
}
Output:
1 2 3 4 5 6 7 8 9 10
Some other structures of for loop:
int i=1;
for(;i<=10;i++)
{
cout<<2*i<<"\t";
}
output:
2 4 6 8 10 12 14 16 18 20
---------------------------------------
---------------------------------------
int i=0;
for(;i<10;)
{
cout<<2*++i;
}
output:
2 4 6 8 10 12 14 16 18 20
Nested Loop
Nested Loop means loop within loop
.An inner loop within the body of an outer loop.
let's see example:
int inner,outer;
for(outer=1;outer<3;outer++)
{
for(inner=1;inner<=3;inner++)
{
cout<<"outer: "<<outer<<"\t"<<"inner: "<<inner;
}
cout<<endl
}
Output:-
outer: 1 inner1 outer: 1 inner2 outer: 1 inner3
outer: 2 inner1 outer: 2 inner2 outer: 2 inner3
The outer loop changes only after inner loop is complety finished.
Syntax for nested while loop
while(condition) //outer loop
{
statements;
while(condition) //inner loop
{
statements;
}
statements;
}
Syntax for nested do-while loop
do //outer loop
{
statements;
do //inner loop
{
statements;
}while(condition);
statements;
}while(condition);
Thanks for reading this very simple and little tutorial. I hope it helps to c++ Beginners.