In this presentation slides you will able to understand easily ,this slides contain loops of c++ programming language which contain for loop , while loop , do while loop and nested these all are describe with definition,examples and flow charts
1. LOOP IN C++:
The mechanism through which a statement or set of statement
can be executed repeatedly is called loop .
The statements that are executed repeatedly are called body of
loop.
The body of loop can be executed repeatedly for a specified
number of times or until the given condition remains true .
In c++ there are four types of loop as;
1) For loop
2) while loop
3) Do while loop
4) Nested loop
@Muhammad
2. –For loop
• The “for” loop is used to execute a
statement or set of statements repeatedly
for a specific numbers of times
• This loop is also known as counter loop.
Syntax of for loop
For{initialization;condition;increment/decrement}
{
}
Statements;
Next statement;
@Muhamm
3. The “for”loop is executed as follows;
Working of
For loop
When control enters into the loop
1)First initialization part is executed . It is executed only for the first time
2)After executing initialization part , the condition is evaluated.if it is
True,then body of loop is executed.
3)After executing body of loop, increament/decreament part is executed
increament/decreament part is last statement of body of “for” loop. .
@Muhammad
4. • Our Services
• Insert the title of your subtitle Here
Flow chart of for loop
intialization
Body of loop
Increament/decreament
Next statement
True
False
@Muhamm
5. Example
of for loop
#include <iostream.h>
#include<conio.h>
Main();
{
Int n;
For (n=1;n<=10;n++)
cout <<n<<endl;
Cout<<“ok”;
}
@Muhammad
6. While loop
• The “while loop ” is conditional loop structure it is used to
Execute a statement or set of statement as long as the given
condtioin remain true
• This loop structure is used when the programmer does not know in
advance
Syntax of while loop;
While (condition)
{
statement;
}
Next – statement;
@Muhammad
7. The “for”loop is executed as follows;
• When a “while ”loop statement is executed the condition
is evaluate first
• If the condition is true then body of loop is executed
• After executing the body of loop, the execution control goes back to t
he “while”statement and evaluates the condition again.
• If the given condition is still true , the body of loop is executed again.
This process is repeated.
• When the given condition become false at any stage during
• execution ,the loop is terminated.
@Muhamma
8. • Our Services
• Insert the title of your subtitle Here
Flow chart of ”while”loop
Body of loop
Next statement
False
True
@Muhamm
9. Example of “while ” loop
#include<iostream.h>
#include<conio.h>
Main()
{
Int num;
Num=1;
While(num<=15)
{
If (num%3==0)
Cout<<num<<endl;
Num=num+1;
}
}
@Muhammad
10. DO WHILE LOOP;
• as while loop ,do while loop is also used to execute a state
ment or set of statement as long as the given condition rem
ains true
• The condition comes after the body of loop
• The condition is evaluate after executing the body of loop
The body of loop must be executed at least once even if
The condition is false
• Semicolon(;) is given after the while (condition)
Syntax of Do while loop;
Initialize
Do {
Body of loop
Updation
}
While (condition) @Muhammad
11. Flow chart of ”Do while”loop
Body of loop
Next statement
False
True
@Muhammad
12. Example of “Do while ”
loop
#include<iostream.h>
#include<conio.h>
Main()
{
Int i=1;
Do
{
Cout<<i<<“/n”;
i++;
}
While (1<=10);
}
@Muhamma
13. Nested loop;
• A nested loop is a loop within a loop, an inner loop within
the body of an outer one.
• How this works is that the first pass of the outer loop triggers
the inner loop, which executes to completion. Then the
second pass of the outer loop triggers the inner loop again.
• This repeats until the outer loop finishes
Syntax of nested loop;
Outer loop
{
statement (s)-1
Inner-loop
{
Body of inner loop
}
Statement (s)-2
}
Next – statement;
@Muhammad
14. The “nested”loop is executed as follows;
• The condition given in outer is evaluated.if condition is true
Controls enters inside the body of outer loop.otherwise loop is
Terminated.
• If body of outer loop contains any statement before the inner
loop, it is executed .
@Muhammad