[go: up one dir, main page]

0% found this document useful (0 votes)
20 views23 pages

Presentation of Nested Loop

The document provides an overview of nested loops in programming, specifically in C++. It describes the three types of nested loops: nested for loop, nested while loop, and nested do-while loop, along with their syntax, working, and examples. Additionally, it highlights the advantages of using nested loops for handling multi-dimensional data structures and complex iterations.

Uploaded by

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

Presentation of Nested Loop

The document provides an overview of nested loops in programming, specifically in C++. It describes the three types of nested loops: nested for loop, nested while loop, and nested do-while loop, along with their syntax, working, and examples. Additionally, it highlights the advantages of using nested loops for handling multi-dimensional data structures and complex iterations.

Uploaded by

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

‫ِبْس ِم الَّلِه الَّر ْح َم ِن الَّر ِح يم‬

In the name of Allah who is the most


kind and the most merciful.
UNIVERSITY OF CENTRAL PUNJAB:
Presentation:
Name:
Muhammad Hasnain (46)
Salman Naeem (27)
CLASS:
Bscs(1)
Submitted by:
prof.Muhammad waqas
NESTED LOOP:
Types of Nested Loop:
1.Nested For Loop.
2.Nested While loop.
3.Nested do-while-loop
Loop:
• Incomputer programming, a type of structure that repeat a
statement or set of statements is known as looping structure.
• It is also known as iterative or repetitive structure.
• Types of Loop:
1. For loop
2. while loop
3. do while loop
Nested Loop:
A loop within a loop is called nested loop.
Working:
In nested loop , loop is executed only when outer condition is true.
So, control enter into inner loop and execute the statement until
condition remain true. If outer loop is wrong so control not enter
into inner loop.
There are three type of nested loop in C++ language .
1. Nested for Loop
2. Nested while loop.
3.Nested do-while-loop.
For Example:
• Write a program to show this series by using nested loop

• #include<iostream>
• using namespace std;
• int main()
• {
• int i,j;
• for(i=1;i<=5;i++)
• {
• for(j=1;j<=5;j++)
• cout<<"*";
• cout<<endl;
• }
• }
Output:

* * * * *
* * * * *
* * * * *
* * * * *
1.Nested For Loop:
It can be defined as a loop with in another for loop is called nested for loop.
Nested loop is mostly used for dealing two-dimensional array.
It is most widely used loop in nested loop.
Syntax:
for(initialization; condition; increment / decrement) //outer loop
{
for (initialization; condition; increment / decrement) //inner loop
{
statement;
}
}
Flowchart
Of Nested For
Loop:
For Example:
• Write a program to show this series by using nested For loop
#include<iostream>
using namespace std;
int main()
{
int i , j;
for(i=1 ; i<=5; i++) //outer loop
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
Output:

*
* *
* **
* ***
* ****
2.Nested while-loop :
•A nested while loop is a while statement in which another while
statement is called nested while statement.

• Working:

In a nested while loop, one iteration of the outer loop is first


executed, after which the inner loop is executed. The execution of
the inner loop continues till the condition described in the inner
loop is satisfied.
Syntax :
while( outer condition)
statement;
{
while(inner condition)
{
statement;
}
}
Flowchart
Of Nested
While Loop:
Example:
• Write a program to show a sequence of number of series as follows:
12345
12345
12345

#include<iostream>
using namespace std;
int main()
{
int n , I , j;
cout<<“Enter numbers of rows:”;
cin>>n;

i=1;
while(i<=n) //outer loop for printing rows
{
while(j<=5) //inner loop for printing numbers
{
cout<<j<<" ";
j++;

}
cout<<endl;
i++;
return 0;
}
}
Output :
12345
12345
12345
3.Nested do-while-loop:
• Nested do while loop is a loop in which another do while loop.
• It is loop in which another loop.
• In this loop structure , inner loop executes multiple times for each
iteration of outer loop
• Syntax:

do {
statement(s);
do
statement(s);
}
while(condition);
}
while(condition);

Working:
Nested do while loop is a type of nested loop in which inner loop is executed
only when outer loop is true.

Example:
Write a program to show this sequence by using nested do while loop.

1
1 2
1 23
1 234
1 2345
#include<iostream>
using namespace std;
int main()
{

int i , j , n ;
cout<<“Enter numbers of rows:”;
cin>>n
i=1;
do
{
j=1;
do
{ //inner loop body.
cout<<j<<“ “;
j++;
}
while(j<=i); //for printing numbers
cout<<endl;
i++;
}
while(i<=n); //for printing rows
return 0;
}
Flowchart
Of Nested Do-
While Loop:
Advantages of Nested loop:
• They can be useful for iterating through multi-dimensional
data
structures, such as 2 D array.
• It allow developer to use multiple complex iteration and
repetitive tasks with greater efficiency.
• Nested
loop allow user to iterate over element with in
elements
• For loop is mostly used in nested loop
Any Question?
Thank You Very Much For Your
Attention……

You might also like