SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
Nested Loops in C
• C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.
• Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any
number of loops. The nesting level can be defined at n times. You can define any type of loop inside
another loop; for example, you can define 'while' loop inside a 'for' loop.
• Syntax of Nested loop
1. Outer_loop
2. {
3. Inner_loop
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }
• Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.
Nested for loop
The nested for loop means any type of loop which is defined inside the 'for' loop.
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
Example of nested for loop
#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%dt",(i*j)); // printing the value.
}
printf("n");
}
Explanation of the above code
• First, the 'i' variable is initialized to 1 and then program control
passes to the i<=n.
• The program control checks whether the condition 'i<=n' is true or
not.
• If the condition is true, then the program control passes to the inner
loop.
• The inner loop will get executed until the condition is true.
• After the execution of the inner loop, the control moves back to the
update of the outer loop, i.e., i++.
• After incrementing the value of the loop counter, the condition is
checked again, i.e., i<=n.
• If the condition is true, then the inner loop will be executed again.
• This process will continue until the condition of the outer loop is true.
Output
Nested while loop
• The nested while loop means any type of loop which is defined
inside the 'while' loop.
while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}
Example of nested while loop
#include <stdio.h>
int main()
{
int rows; // variable declaration
int columns; // variable declaration
int k=1; // variable initialization
printf("Enter the number of rows :"); // input the number of rows.
scanf("%d",&rows);
printf("nEnter the number of columns :"); // input the number of columns.
scanf("%d",&columns);
int a[rows][columns]; //2d array declaration
int i=1;
while(i<=rows) // outer loop
{
int j=1;
while(j<=columns) // inner loop
{
printf("%dt",k); // printing the value of k.
k++; // increment counter
j++;
}
i++;
printf("n");
}
}
Explanation of the above code.
• We have created the 2d array, i.e., int a[rows][columns].
• The program initializes the 'i' variable by 1.
• Now, control moves to the while loop, and this loop checks whether
the condition is true, then the program control moves to the inner
loop.
• After the execution of the inner loop, the control moves to the update
of the outer loop, i.e., i++.
• After incrementing the value of 'i', the condition (i<=rows) is checked.
• If the condition is true, the control then again moves to the inner
loop.
• This process continues until the condition of the outer loop is true.
Output
Nested do..while loop
The nested do..while loop means any type of loop which is defined
inside the 'do..while' loop.
do
{
do
{
// inner loop statements.
}while(condition);
// outer loop statements.
}while(condition);
Example of nested do..while loop.
#include <stdio.h>
int main()
{
/*printing the pattern
********
********
********
******** */
int i=1;
do // outer loop
{
int j=1;
do // inner loop
{
printf("*");
j++;
}while(j<=8);
printf("n");
i++;
}while(i<=4);
}
Example
Explanation of the above code.
• First, we initialize the outer loop counter variable, i.e., 'i' by 1.
• As we know that the do..while loop executes once without
checking the condition, so the inner loop is executed without
checking the condition in the outer loop.
• After the execution of the inner loop, the control moves to the
update of the i++.
• When the loop counter value is incremented, the condition is
checked. If the condition in the outer loop is true, then the inner
loop is executed.
• This process will continue until the condition in the outer loop is
true.

More Related Content

PPTX
Forloop
PPTX
Loops in C
PPTX
Loops in c programming
PPTX
If else statement in c++
PPTX
While , For , Do-While Loop
PPTX
Conditional statement c++
PPTX
Decision making statements in C programming
PPTX
Loops in c language
Forloop
Loops in C
Loops in c programming
If else statement in c++
While , For , Do-While Loop
Conditional statement c++
Decision making statements in C programming
Loops in c language

What's hot (20)

PPSX
Break and continue
PPTX
Loops c++
PPT
Functions in c++
PPT
Control structure C++
PDF
Unit II chapter 4 Loops in C
PPTX
The Loops
PPTX
If statements in c programming
PPTX
Nested loop in C language
PDF
Function in C
PPSX
C lecture 4 nested loops and jumping statements slideshare
PPTX
Looping statements in C
PPTX
Decision Making and Looping
PPTX
Switch statement, break statement, go to statement
PPTX
Loop(for, while, do while) condition Presentation
PPT
File handling in C++
PPTX
The string class
PPTX
Function in C program
PPTX
Control Flow Statements
PDF
Chapter 2 : Balagurusamy_ Programming ANsI in C
Break and continue
Loops c++
Functions in c++
Control structure C++
Unit II chapter 4 Loops in C
The Loops
If statements in c programming
Nested loop in C language
Function in C
C lecture 4 nested loops and jumping statements slideshare
Looping statements in C
Decision Making and Looping
Switch statement, break statement, go to statement
Loop(for, while, do while) condition Presentation
File handling in C++
The string class
Function in C program
Control Flow Statements
Chapter 2 : Balagurusamy_ Programming ANsI in C
Ad

Similar to Nested Loops in C.pptx (20)

PPTX
Presentation on nesting of loops
DOCX
Nested Loops in C unit2.docx
PPT
12 lec 12 loop
PPTX
Loops in c
PPSX
Nested loops
PPT
Chapter06.PPT
PDF
presentationonnestingofloops-110228071935-phpapp01.pdf
PDF
04-Looping( For , while and do while looping) .pdf
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
PPTX
Loops in c language
PDF
175035 cse lab-05
PDF
loops in C ppt.pdf
PPTX
Loop (Computer programming and utilization)
PPTX
C Programming Language Part 6
PPTX
Cse lecture-7-c loop
PPTX
Loops Basics
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPSX
C lecture 3 control statements slideshare
Presentation on nesting of loops
Nested Loops in C unit2.docx
12 lec 12 loop
Loops in c
Nested loops
Chapter06.PPT
presentationonnestingofloops-110228071935-phpapp01.pdf
04-Looping( For , while and do while looping) .pdf
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
Loops in c language
175035 cse lab-05
loops in C ppt.pdf
Loop (Computer programming and utilization)
C Programming Language Part 6
Cse lecture-7-c loop
Loops Basics
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
C lecture 3 control statements slideshare
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Cell Structure & Organelles in detailed.
A systematic review of self-coping strategies used by university students to ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Paper A Mock Exam 9_ Attempt review.pdf.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
UNIT III MENTAL HEALTH NURSING ASSESSMENT

Nested Loops in C.pptx

  • 1. Nested Loops in C • C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Let's observe an example of nesting loops in C. • Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. • Syntax of Nested loop 1. Outer_loop 2. { 3. Inner_loop 4. { 5. // inner loop statements. 6. } 7. // outer loop statements. 8. } • Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.
  • 2. Nested for loop The nested for loop means any type of loop which is defined inside the 'for' loop. for (initialization; condition; update) { for(initialization; condition; update) { // inner loop statements. } // outer loop statements. }
  • 3. Example of nested for loop #include <stdio.h> int main() { int n;// variable declaration printf("Enter the value of n :"); // Displaying the n tables. for(int i=1;i<=n;i++) // outer loop { for(int j=1;j<=10;j++) // inner loop { printf("%dt",(i*j)); // printing the value. } printf("n"); }
  • 4. Explanation of the above code • First, the 'i' variable is initialized to 1 and then program control passes to the i<=n. • The program control checks whether the condition 'i<=n' is true or not. • If the condition is true, then the program control passes to the inner loop. • The inner loop will get executed until the condition is true. • After the execution of the inner loop, the control moves back to the update of the outer loop, i.e., i++. • After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n. • If the condition is true, then the inner loop will be executed again. • This process will continue until the condition of the outer loop is true.
  • 6. Nested while loop • The nested while loop means any type of loop which is defined inside the 'while' loop. while(condition) { while(condition) { // inner loop statements. } // outer loop statements. }
  • 7. Example of nested while loop #include <stdio.h> int main() { int rows; // variable declaration int columns; // variable declaration int k=1; // variable initialization printf("Enter the number of rows :"); // input the number of rows. scanf("%d",&rows); printf("nEnter the number of columns :"); // input the number of columns. scanf("%d",&columns); int a[rows][columns]; //2d array declaration int i=1; while(i<=rows) // outer loop { int j=1; while(j<=columns) // inner loop { printf("%dt",k); // printing the value of k. k++; // increment counter j++; } i++; printf("n"); } }
  • 8. Explanation of the above code. • We have created the 2d array, i.e., int a[rows][columns]. • The program initializes the 'i' variable by 1. • Now, control moves to the while loop, and this loop checks whether the condition is true, then the program control moves to the inner loop. • After the execution of the inner loop, the control moves to the update of the outer loop, i.e., i++. • After incrementing the value of 'i', the condition (i<=rows) is checked. • If the condition is true, the control then again moves to the inner loop. • This process continues until the condition of the outer loop is true.
  • 10. Nested do..while loop The nested do..while loop means any type of loop which is defined inside the 'do..while' loop. do { do { // inner loop statements. }while(condition); // outer loop statements. }while(condition);
  • 11. Example of nested do..while loop. #include <stdio.h> int main() { /*printing the pattern ******** ******** ******** ******** */ int i=1; do // outer loop { int j=1; do // inner loop { printf("*"); j++; }while(j<=8); printf("n"); i++; }while(i<=4); }
  • 13. Explanation of the above code. • First, we initialize the outer loop counter variable, i.e., 'i' by 1. • As we know that the do..while loop executes once without checking the condition, so the inner loop is executed without checking the condition in the outer loop. • After the execution of the inner loop, the control moves to the update of the i++. • When the loop counter value is incremented, the condition is checked. If the condition in the outer loop is true, then the inner loop is executed. • This process will continue until the condition in the outer loop is true.