0% found this document useful (0 votes)
22 views15 pages

Loops in C++

The document discusses different types of loops in C++ including for, while, do-while and nested loops. It provides the syntax and flowchart for each loop and examples of their usage.

Uploaded by

fishatsion09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views15 pages

Loops in C++

The document discusses different types of loops in C++ including for, while, do-while and nested loops. It provides the syntax and flowchart for each loop and examples of their usage.

Uploaded by

fishatsion09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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
–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
The “for”loop is executed as follows;
1) First initialization part is executed . It is executed only for the first time
When control enters into the loop

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. .

Working of
For loop
@Muhamma
Flow chart of for loop
False
intialization

True

Body of loop

Increament/decreament

Next statement @Muham


Example
of for loop
#include <iostream>
using namespace std;
int main()
{ int n;for
(n=1;n<=10;n++)
cout<<n<<endl;
cout<<"ok";}

@Muhammad
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;
@Muhamma
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
• Our Services
Flow chart of ”while”loop
• Insert the title of your subtitle Here

False

True

Body of loop

Next statement
@Muham
Example of “while ” loop
#include <iostream>
using namespace
std;
int main()
{ int num;
num=1;
while(num<=15)

{if (num%3==0)
cout<<num<<endl;
num=num+1;
}}

@Muhamma
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) @Muhamma
Flow chart of ”Do while”loop

Body of loop

True

False

Next statement
@Muhamma
Example of “Do while ”
loop
#include <iostream>
using namespace
std;
int main()
{ int i=1; do
{cout<<i<<"\n";i++;}
while (i<=10);}

@Muhamma
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;
@Muhamma
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 .

@Muhamma
Example of “nested”
loop
#include<iostream.h>
#include<conio.h>
Main()
{
Int u,i;
U=1;
While (u<=3)
{
Cout <<u<<endl;
For (i=1;i<=2;i++)
Cout <<“I love Pakistan\n”;
U=u+1;
}
}

@Muhamma

You might also like