SlideShare a Scribd company logo
Nested Loops
• What is a nested loop?
• Types of nested loops
• Working of a nested loop
• Example Programs
What are nested loops?
• A nested loop is a loop
inside the body of
another loop.
• The nested loop is
known as the inner loop
and the loop in which it
is nested is known as the
outer loop
for ( int i=0; i< 3; i++)
{
for (int j = 0; j<3; j++)
cout<< j << “ “;
}
Outer loop
Inner loop
Outerloopbody
Innerloopbody
Types of Nested Loops
Nested for Nested while Nested do while Mixed
Nested for loop
for ( initialization; test; update) outer loop
{ //outer loop statements;
for( initialization; test; update) inner loop
{
//inner loop statements;
}
}
Nested while loop
while ( test) outer loop
{ //outer loop statements;
while( test) inner loop
{
//inner loop statements;
}
}
Nested do …. while loop
do outer loop
{ //outer loop statements;
do inner loop
{
//inner loop statements;
} while (test);
} while(test);
Working of nested loops : example 1
For each iteration of the outer loop, the inner loop is completely executed.
So for example for the following nested loop:
for(int i=0; i<3;i++)
for(int j=0; j<3;j++)
cout<<j<<“ ” ;
the statement, cout<<j<<“ ” ,will execute 3X3=9 times. For each iteration of
outer loop( i = 0, 1, 2) , inner loop will execute 3 times(j= 0, 1, 2).
Also note that there are no brackets. This is because both loops contain
single statements; outer loop contains inner loop and inner loop contains
single cout statement.
Working of nested loops : example 2
If the loop contains multiple statements then there is a need for brackets.
For the following nested loops:
for(int i=0; i<2;i++)
{ cout<<“n”;
for(int j=0; j<3;j++)
cout<<j<<“ ” ;
}
the statement, cout<<j<<“ ” ,will execute 2X3=6 times. For each iteration of outer
loop( i = 0, 1) , inner loop will execute 3 times(j= 0, 1, 2).
There are brackets in the above nested loop. This is because the outer loop
contains two statements; one cout<<“n” and the inner loop . The inner loop
contains single cout statement thus there are no brackets for inner loop.
Working of nested loops : example 3
If the loop contains multiple statements then there is a need for brackets.
For the following nested loops:
for(int i=0; i< 4;i++)
{ cout<<“n”;
for(int j=1; j<= 2;j++)
{cout<<j<<“ ” ;
cout<<“inner loop”;
}}
There are brackets in both the above nested loop. This is because the outer loop contains
two statements; one cout<<“n” and the inner loop . The inner loop contains two
statements 1.cout<<j<<“”and cout <<“inner loop “.
For each iteration of outer loop( i = 0, 1,2,3) , inner loop will execute 2 times(j= 1, 2). So
total no of times= 4X2=8
Working of Nested Loops
for(int i= 0 ; i< 2; i++)
{
cout<<“n”;
for(int j = 0; j<2; j ++)
cout<< j << “ ”;
}
1 2
3
4 5
6
8
7
1. STEP 1:The outer loop is initialized with value of i as 0
2. STEP 2: Value of i is tested, since the condition is true( i<2), the loop
is entered
3. STEP 3:A newline is displayed (cout<<“n”;). This is part of outer loop.
4. STEP 4 :The control goes to inner loop, where j is initialized with 0
5. STEP 5: Value of j is tested, j<2 is true, inner loop is entered
6. STEP 6: The statement cout << j << “”is executed, value of j is displayed
7. STEP 7: The value of j is incremented.
8. Now STEPs 5, 6 and 7 are repeated till the condition ,j<2,
becomes false.
9. When value of j is 2, control comes out of inner loop.
8. STEP 8: Now the control goes to outer loop update statement,
i is incremented.
9. STEPs 2 – 7 are repeated. The steps are repeated for value of i = 1
This continues till value of i becomes 2. Then the outer loop is
terminated.
Working of nested loops
Output:
0 1
0 1
for(int i= 0 ; i< 2; i++)
{
cout<<“n”;
for(int j = 0; j<2; j ++)
cout<< j << “ ”;
}
0
i
1
0
1
j
0
1
i
1 2
3
6
4 5 7
8
2
2 2
i
Example Programs
Program: To print the pattern
#include<iostream.h>
void main()
{
for( int x = 1; x < 3; x ++)
{
for ( int y = 1; y <= x; y ++)
cout<<y <<”t”;
cout<< ”n” ;
}}
1
1 2
1 2 3
Explanation
1.Outer loop : 1st Iteration x=1
1.1 Inner Loop : 1st Iteration,y=1 cout<<y<<“t
Increment y , y=2, check y<=x=false Inner loop over
2. Outer loop :2nd Iteration x=2
2.1 Inner loop : 1st Iteration ,y=1 cout<< y<<“t”
2.2 Inner loop : 2nd Iteration, y=2 cout<<y<<“t”
Increment y , y=3, check y<=x=false Inner loop over
3. Outer loop :3rd Iteration x=3
3.1 Inner loop : 1st Iteration y=1, cout<<y<<“t”
3.2 Inner loop : 2nd Iteration, y=2, cout<<y<<“t”
3.3 Inner loop : 3rd Iteration , y = 3, cout<<y<<“t”
Increment y , y=4, check y<=x=false Inner loop over
increment x, x= 3, outer loop over
Output
Program :Program to find the divisors of numbers
entered.
#include<iostream.h>
#include<conio.h>
void main()
{ int n, k;
cout<< “Enter the number of integers :”;
cin >> n;
for( int i = 0; i < n; i ++)
{ cout<< “Enter the number of whose divisor are to be found”;
cin >> k;
cout << “n The divisor are :”<<”n”;
for ( int j = 1; j <= k/2; j ++)
if( k%j == 0)
cout<< j <<”t”;
}
EXPLANATION:
The outer loop keeps track of the
number of integer a user inputs.
The inner loop calculates and displays
its divisors
Enter the number of integers : 2
Enter the number whose divisor is to be found : 6
The divisors are :
1 2 3
Enter the number whose divisor is to be found : 15
The divisors are :
1 3 5
Output
Program : Display the multiplication table of a number till the
user wishes
#include<iostream.h>
#include<conio.h>
void main()
{
int num, l;
char ch;
do
{cout<<”Enter a number whose multiplication table is to be displayed:
”;
cin>>num;
cout<<”Enter the limit of upto which table is to be displayed :”;
cin>>l;
int i = 1;
while( i <= l){
{
cout<<num<<”X”<< i <<”=”<<num*i <<”n”;}
cout<<”Do you wish to continue (Y/N)”<<”n”;
cin>>ch; } while(ch!=’n’||ch!=’N’);}}
Enter a number whose multiplication table is to
be displayed: 8
Enter the limit upto which table is to be
displayed : 4
8X1=8
8X2=16
8X3=24
8X4=32
Do you wish to continue(Y/N)
Y
Enter a number whose multiplication table is to
be displayed: 6
Enter the limit upto which table is to be
displayed : 5
6X1=6
6X2=12
6X3=18
6X4=24
6X5=30
Do you wish to continue(Y/N)
N
Output
Nested loops

More Related Content

What's hot (20)

Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
Vraj Patel
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
C if else
C if elseC if else
C if else
Ritwik Das
 

Similar to Nested loops (20)

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
ShifatiRabbi
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
TAlha MAlik
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
pratikborsadiya
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
AliaaAqilah3
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
Mohammad Shaker
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Programming Fundamentals presentation slide
Programming Fundamentals presentation slideProgramming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
Ch4
Ch4Ch4
Ch4
aamirsahito
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
Mukund Trivedi
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
AliaaAqilah3
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
Mohammad Shaker
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Programming Fundamentals presentation slide
Programming Fundamentals presentation slideProgramming Fundamentals presentation slide
Programming Fundamentals presentation slide
mibrahim020205
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 

More from Neeru Mittal (18)

Using the Word Wheel to Learn basic English Vocabulary
Using the Word Wheel to Learn basic English VocabularyUsing the Word Wheel to Learn basic English Vocabulary
Using the Word Wheel to Learn basic English Vocabulary
Neeru Mittal
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
Neeru Mittal
 
Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptx
Neeru Mittal
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
Neeru Mittal
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
Neeru Mittal
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
Neeru Mittal
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
Neeru Mittal
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
Neeru Mittal
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Arrays
ArraysArrays
Arrays
Neeru Mittal
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
Neeru Mittal
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
Neeru Mittal
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
Neeru Mittal
 
Using the Word Wheel to Learn basic English Vocabulary
Using the Word Wheel to Learn basic English VocabularyUsing the Word Wheel to Learn basic English Vocabulary
Using the Word Wheel to Learn basic English Vocabulary
Neeru Mittal
 
Introduction to AI and its domains.pptx
Introduction to AI and its domains.pptxIntroduction to AI and its domains.pptx
Introduction to AI and its domains.pptx
Neeru Mittal
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
Neeru Mittal
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
Neeru Mittal
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
Neeru Mittal
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
Neeru Mittal
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
Neeru Mittal
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
Neeru Mittal
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
Neeru Mittal
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
Neeru Mittal
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
Neeru Mittal
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
Neeru Mittal
 

Recently uploaded (20)

ASP.NET Interview Questions PDF By ScholarHat
ASP.NET  Interview Questions PDF By ScholarHatASP.NET  Interview Questions PDF By ScholarHat
ASP.NET Interview Questions PDF By ScholarHat
Scholarhat
 
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Inventory Reporting in Odoo 17 -  Odoo 17 Inventory AppInventory Reporting in Odoo 17 -  Odoo 17 Inventory App
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Celine George
 
How to create security group category in Odoo 17
How to create security group category in Odoo 17How to create security group category in Odoo 17
How to create security group category in Odoo 17
Celine George
 
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
SONU HEETSON
 
Unit 1 Computer Hardware for Educational Computing.pptx
Unit 1 Computer Hardware for Educational Computing.pptxUnit 1 Computer Hardware for Educational Computing.pptx
Unit 1 Computer Hardware for Educational Computing.pptx
RomaSmart1
 
PUBH1000 - Module 2: Public Health History
PUBH1000 - Module 2: Public Health HistoryPUBH1000 - Module 2: Public Health History
PUBH1000 - Module 2: Public Health History
Jonathan Hallett
 
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdfNUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
Dolisha Warbi
 
Administrative bodies( D and C Act, 1940
Administrative bodies( D and C Act, 1940Administrative bodies( D and C Act, 1940
Administrative bodies( D and C Act, 1940
P.N.DESHMUKH
 
teacher activies un classroom and students
teacher activies un classroom and studentsteacher activies un classroom and students
teacher activies un classroom and students
prabowoedy1
 
Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18
Celine George
 
Helping Autistic Girls Shine Webinar Slides
Helping Autistic Girls Shine Webinar SlidesHelping Autistic Girls Shine Webinar Slides
Helping Autistic Girls Shine Webinar Slides
Pooky Knightsmith
 
Oral exam Kenneth Bech - What is the meaning of strategic fit?
Oral exam Kenneth Bech - What is the meaning of strategic fit?Oral exam Kenneth Bech - What is the meaning of strategic fit?
Oral exam Kenneth Bech - What is the meaning of strategic fit?
MIPLM
 
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
 
Cyrus_Kelisha_SMM_PB1_2024-November.pptx
Cyrus_Kelisha_SMM_PB1_2024-November.pptxCyrus_Kelisha_SMM_PB1_2024-November.pptx
Cyrus_Kelisha_SMM_PB1_2024-November.pptx
KelishaCyrus
 
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAHBISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
coacharyasetiyaki
 
Mastering Soft Tissue Therapy & Sports Taping
Mastering Soft Tissue Therapy & Sports TapingMastering Soft Tissue Therapy & Sports Taping
Mastering Soft Tissue Therapy & Sports Taping
Kusal Goonewardena
 
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
SONU HEETSON
 
Interim Guidelines for PMES-DM-17-2025-PPT.pptx
Interim Guidelines for PMES-DM-17-2025-PPT.pptxInterim Guidelines for PMES-DM-17-2025-PPT.pptx
Interim Guidelines for PMES-DM-17-2025-PPT.pptx
sirjeromemanansala
 
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
Prof. (Dr.) Vinod Kumar Kanvaria
 
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptxCRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
PoojaSen20
 
ASP.NET Interview Questions PDF By ScholarHat
ASP.NET  Interview Questions PDF By ScholarHatASP.NET  Interview Questions PDF By ScholarHat
ASP.NET Interview Questions PDF By ScholarHat
Scholarhat
 
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Inventory Reporting in Odoo 17 -  Odoo 17 Inventory AppInventory Reporting in Odoo 17 -  Odoo 17 Inventory App
Inventory Reporting in Odoo 17 - Odoo 17 Inventory App
Celine George
 
How to create security group category in Odoo 17
How to create security group category in Odoo 17How to create security group category in Odoo 17
How to create security group category in Odoo 17
Celine George
 
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
RRB ALP CBT 2 RAC Question Paper MCQ (Railway Assistant Loco Pilot)
SONU HEETSON
 
Unit 1 Computer Hardware for Educational Computing.pptx
Unit 1 Computer Hardware for Educational Computing.pptxUnit 1 Computer Hardware for Educational Computing.pptx
Unit 1 Computer Hardware for Educational Computing.pptx
RomaSmart1
 
PUBH1000 - Module 2: Public Health History
PUBH1000 - Module 2: Public Health HistoryPUBH1000 - Module 2: Public Health History
PUBH1000 - Module 2: Public Health History
Jonathan Hallett
 
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdfNUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
NUTRITIONAL ASSESSMENT AND EDUCATION - 5TH SEM.pdf
Dolisha Warbi
 
Administrative bodies( D and C Act, 1940
Administrative bodies( D and C Act, 1940Administrative bodies( D and C Act, 1940
Administrative bodies( D and C Act, 1940
P.N.DESHMUKH
 
teacher activies un classroom and students
teacher activies un classroom and studentsteacher activies un classroom and students
teacher activies un classroom and students
prabowoedy1
 
Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18Effective Product Variant Management in Odoo 18
Effective Product Variant Management in Odoo 18
Celine George
 
Helping Autistic Girls Shine Webinar Slides
Helping Autistic Girls Shine Webinar SlidesHelping Autistic Girls Shine Webinar Slides
Helping Autistic Girls Shine Webinar Slides
Pooky Knightsmith
 
Oral exam Kenneth Bech - What is the meaning of strategic fit?
Oral exam Kenneth Bech - What is the meaning of strategic fit?Oral exam Kenneth Bech - What is the meaning of strategic fit?
Oral exam Kenneth Bech - What is the meaning of strategic fit?
MIPLM
 
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
 
Cyrus_Kelisha_SMM_PB1_2024-November.pptx
Cyrus_Kelisha_SMM_PB1_2024-November.pptxCyrus_Kelisha_SMM_PB1_2024-November.pptx
Cyrus_Kelisha_SMM_PB1_2024-November.pptx
KelishaCyrus
 
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAHBISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
BISNIS BERKAH BERANGKAT KE MEKKAH ISTIKMAL SYARIAH
coacharyasetiyaki
 
Mastering Soft Tissue Therapy & Sports Taping
Mastering Soft Tissue Therapy & Sports TapingMastering Soft Tissue Therapy & Sports Taping
Mastering Soft Tissue Therapy & Sports Taping
Kusal Goonewardena
 
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
RRB ALP CBT 2 Mechanic Motor Vehicle Question Paper (MMV Exam MCQ)
SONU HEETSON
 
Interim Guidelines for PMES-DM-17-2025-PPT.pptx
Interim Guidelines for PMES-DM-17-2025-PPT.pptxInterim Guidelines for PMES-DM-17-2025-PPT.pptx
Interim Guidelines for PMES-DM-17-2025-PPT.pptx
sirjeromemanansala
 
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
AI and Academic Writing, Short Term Course in Academic Writing and Publicatio...
Prof. (Dr.) Vinod Kumar Kanvaria
 
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptxCRITICAL THINKING AND NURSING JUDGEMENT.pptx
CRITICAL THINKING AND NURSING JUDGEMENT.pptx
PoojaSen20
 

Nested loops

  • 1. Nested Loops • What is a nested loop? • Types of nested loops • Working of a nested loop • Example Programs
  • 2. What are nested loops? • A nested loop is a loop inside the body of another loop. • The nested loop is known as the inner loop and the loop in which it is nested is known as the outer loop for ( int i=0; i< 3; i++) { for (int j = 0; j<3; j++) cout<< j << “ “; } Outer loop Inner loop Outerloopbody Innerloopbody
  • 3. Types of Nested Loops Nested for Nested while Nested do while Mixed
  • 4. Nested for loop for ( initialization; test; update) outer loop { //outer loop statements; for( initialization; test; update) inner loop { //inner loop statements; } }
  • 5. Nested while loop while ( test) outer loop { //outer loop statements; while( test) inner loop { //inner loop statements; } }
  • 6. Nested do …. while loop do outer loop { //outer loop statements; do inner loop { //inner loop statements; } while (test); } while(test);
  • 7. Working of nested loops : example 1 For each iteration of the outer loop, the inner loop is completely executed. So for example for the following nested loop: for(int i=0; i<3;i++) for(int j=0; j<3;j++) cout<<j<<“ ” ; the statement, cout<<j<<“ ” ,will execute 3X3=9 times. For each iteration of outer loop( i = 0, 1, 2) , inner loop will execute 3 times(j= 0, 1, 2). Also note that there are no brackets. This is because both loops contain single statements; outer loop contains inner loop and inner loop contains single cout statement.
  • 8. Working of nested loops : example 2 If the loop contains multiple statements then there is a need for brackets. For the following nested loops: for(int i=0; i<2;i++) { cout<<“n”; for(int j=0; j<3;j++) cout<<j<<“ ” ; } the statement, cout<<j<<“ ” ,will execute 2X3=6 times. For each iteration of outer loop( i = 0, 1) , inner loop will execute 3 times(j= 0, 1, 2). There are brackets in the above nested loop. This is because the outer loop contains two statements; one cout<<“n” and the inner loop . The inner loop contains single cout statement thus there are no brackets for inner loop.
  • 9. Working of nested loops : example 3 If the loop contains multiple statements then there is a need for brackets. For the following nested loops: for(int i=0; i< 4;i++) { cout<<“n”; for(int j=1; j<= 2;j++) {cout<<j<<“ ” ; cout<<“inner loop”; }} There are brackets in both the above nested loop. This is because the outer loop contains two statements; one cout<<“n” and the inner loop . The inner loop contains two statements 1.cout<<j<<“”and cout <<“inner loop “. For each iteration of outer loop( i = 0, 1,2,3) , inner loop will execute 2 times(j= 1, 2). So total no of times= 4X2=8
  • 10. Working of Nested Loops for(int i= 0 ; i< 2; i++) { cout<<“n”; for(int j = 0; j<2; j ++) cout<< j << “ ”; } 1 2 3 4 5 6 8 7 1. STEP 1:The outer loop is initialized with value of i as 0 2. STEP 2: Value of i is tested, since the condition is true( i<2), the loop is entered 3. STEP 3:A newline is displayed (cout<<“n”;). This is part of outer loop. 4. STEP 4 :The control goes to inner loop, where j is initialized with 0 5. STEP 5: Value of j is tested, j<2 is true, inner loop is entered 6. STEP 6: The statement cout << j << “”is executed, value of j is displayed 7. STEP 7: The value of j is incremented. 8. Now STEPs 5, 6 and 7 are repeated till the condition ,j<2, becomes false. 9. When value of j is 2, control comes out of inner loop. 8. STEP 8: Now the control goes to outer loop update statement, i is incremented. 9. STEPs 2 – 7 are repeated. The steps are repeated for value of i = 1 This continues till value of i becomes 2. Then the outer loop is terminated.
  • 11. Working of nested loops Output: 0 1 0 1 for(int i= 0 ; i< 2; i++) { cout<<“n”; for(int j = 0; j<2; j ++) cout<< j << “ ”; } 0 i 1 0 1 j 0 1 i 1 2 3 6 4 5 7 8 2 2 2 i
  • 13. Program: To print the pattern #include<iostream.h> void main() { for( int x = 1; x < 3; x ++) { for ( int y = 1; y <= x; y ++) cout<<y <<”t”; cout<< ”n” ; }} 1 1 2 1 2 3 Explanation 1.Outer loop : 1st Iteration x=1 1.1 Inner Loop : 1st Iteration,y=1 cout<<y<<“t Increment y , y=2, check y<=x=false Inner loop over 2. Outer loop :2nd Iteration x=2 2.1 Inner loop : 1st Iteration ,y=1 cout<< y<<“t” 2.2 Inner loop : 2nd Iteration, y=2 cout<<y<<“t” Increment y , y=3, check y<=x=false Inner loop over 3. Outer loop :3rd Iteration x=3 3.1 Inner loop : 1st Iteration y=1, cout<<y<<“t” 3.2 Inner loop : 2nd Iteration, y=2, cout<<y<<“t” 3.3 Inner loop : 3rd Iteration , y = 3, cout<<y<<“t” Increment y , y=4, check y<=x=false Inner loop over increment x, x= 3, outer loop over Output
  • 14. Program :Program to find the divisors of numbers entered. #include<iostream.h> #include<conio.h> void main() { int n, k; cout<< “Enter the number of integers :”; cin >> n; for( int i = 0; i < n; i ++) { cout<< “Enter the number of whose divisor are to be found”; cin >> k; cout << “n The divisor are :”<<”n”; for ( int j = 1; j <= k/2; j ++) if( k%j == 0) cout<< j <<”t”; } EXPLANATION: The outer loop keeps track of the number of integer a user inputs. The inner loop calculates and displays its divisors Enter the number of integers : 2 Enter the number whose divisor is to be found : 6 The divisors are : 1 2 3 Enter the number whose divisor is to be found : 15 The divisors are : 1 3 5 Output
  • 15. Program : Display the multiplication table of a number till the user wishes #include<iostream.h> #include<conio.h> void main() { int num, l; char ch; do {cout<<”Enter a number whose multiplication table is to be displayed: ”; cin>>num; cout<<”Enter the limit of upto which table is to be displayed :”; cin>>l; int i = 1; while( i <= l){ { cout<<num<<”X”<< i <<”=”<<num*i <<”n”;} cout<<”Do you wish to continue (Y/N)”<<”n”; cin>>ch; } while(ch!=’n’||ch!=’N’);}} Enter a number whose multiplication table is to be displayed: 8 Enter the limit upto which table is to be displayed : 4 8X1=8 8X2=16 8X3=24 8X4=32 Do you wish to continue(Y/N) Y Enter a number whose multiplication table is to be displayed: 6 Enter the limit upto which table is to be displayed : 5 6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 Do you wish to continue(Y/N) N Output