SlideShare a Scribd company logo
1 of 16
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

constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++Vraj Patel
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Structures in c language
Structures in c languageStructures in c language
Structures in c languagetanmaymodi4
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C languageErumShammim
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 

What's hot (20)

constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
String functions in C
String functions in CString functions in C
String functions in C
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Forloop
ForloopForloop
Forloop
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
Data types in C
Data types in CData types in C
Data types in C
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Break and continue
Break and continueBreak and continue
Break and continue
 

Similar to Nested loops

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 workingNeeru Mittal
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAliaaAqilah3
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad 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-ee01083101premrings
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptxNaumanRasheed11
 
how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
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
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptxShimoFcis
 

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
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
Iteration
IterationIteration
Iteration
 
06.Loops
06.Loops06.Loops
06.Loops
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
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
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Ch4
Ch4Ch4
Ch4
 
C++ loop
C++ loop C++ loop
C++ loop
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 

More from 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.pptxNeeru Mittal
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in PythonNeeru Mittal
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python PandasNeeru Mittal
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and TricksNeeru Mittal
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV ConnectivityNeeru Mittal
 
Working of while loop
Working of while loopWorking of while loop
Working of while loopNeeru 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 arraysNeeru 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 programmingNeeru 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
 

More from Neeru Mittal (17)

Machine Learning
Machine LearningMachine Learning
Machine Learning
 
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
 
Brain Storming techniques in Python
Brain Storming techniques in PythonBrain Storming techniques in Python
Brain Storming techniques in Python
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Python Tips and Tricks
Python Tips and TricksPython Tips and Tricks
Python Tips and Tricks
 
Python and CSV Connectivity
Python and CSV ConnectivityPython and CSV Connectivity
Python and CSV Connectivity
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
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++
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
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++
 

Recently uploaded

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

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