Movatterモバイル変換


[0]ホーム

URL:


Open In App

A nested loop means a loop statement inside another loop statement.

1. Nested for Loops

Nested for loop refers to any type of loop that is defined inside a 'for' loop.

C
#include<stdio.h>intmain(){// limit for iintm=4;// limit for jintn=5;// Outer loopfor(inti=0;i<m;i++){printf("i = %d: ",i);// InnerLoopfor(intj=0;j<n;j++){printf("%d ",i*n+j);}printf("\n");}return0;}

Output
i = 0: 0 1 2 3 4 i = 1: 5 6 7 8 9 i = 2: 10 11 12 13 14 i = 3: 15 16 17 18 19

Below is the equivalent flow diagram for nested 'for' loops:

Nested-for-Loop


2. Nested while Loops

A nested while loop refers to any type of loop that is defined inside a 'while' loop.

C
#include<stdio.h>intmain(){intend=5;printf("Pattern Printing using Nested While loop");inti=1;while(i<=end){printf("\n");intj=1;while(j<=i){printf("%d ",j);j=j+1;}i=i+1;}return0;}

Output
Pattern Printing using Nested While loop1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Below is the equivalent flow diagram for nested 'while' loops:

Nested-while-Loop


3. Nested do-while Loops

A nested do-while loop refers to any type of loop that is defined inside a do-while loop.

C
#include<stdio.h>intmain(){inti=1;do{intj=1;do{printf("%d ",j);j++;}while(j<=3);printf("\n");i++;}while(i<=2);return0;}

Output
1 2 3 1 2 3

Below is the equivalent flow diagram for nested 'do-while' loops:

Nested-do-while-Loop

4. Hybrid Nested Loops

A hybrid loop structure is where any type of loop is nested inside another loop type.

C++
#include<stdio.h>intmain(){inti=1;while(i<=2){// Outer while loopfor(intj=1;j<=2;j++){// Inner for loopprintf("i = %d, j = %d\n",i,j);}i++;}return0;}

Output
i = 1, j = 1i = 1, j = 2i = 2, j = 1i = 2, j = 2

Break Inside Nested Loops

Whenever we use a break statement inside the nested loops it breaks the innermost loop only and program control goes to the outer loop. Breaking the inner loop does not affects the outer loops.

C
#include<stdio.h>intmain(){inti=0;for(inti=0;i<5;i++){for(intj=0;j<3;j++){// This inner loop will break when i==3if(i==3){break;}printf("* ");}printf("\n");}return0;}

Output
* * * * * * * * * * * *

In the above program, the first loop will iterate from 0 to 5 but here if i will be equal to 3 it will break and will not print the * as shown in the output. 

Continue Inside Nested Loops

Whenever we use a continue statement inside the nested loops it skips the iteration of the innermost loop only. The outer loop remains unaffected.

C
#include<stdio.h>intmain(){inti=0;for(inti=0;i<2;i++){for(intj=0;j<3;j++){// This inner loop will skip when j==2if(j==2){continue;}printf("%d ",j);}printf("\n");}return0;}

Output
0 1 0 1

In the above program, the inner loop will be skipped when j will be equal to 2. The outer loop will remain unaffected.

Uses of Nested Loops

  • Printing Patterns: Nested loops are often used to print complex patterns such as printing shapes, grids, or tables.
  • Searching and Sorting: Nested loops are used in algorithms that involve searching for or sorting elements like bubble sort, insertion sort, matrix searching etc.
  • Multi-Dimensional Data: Nested loops are useful when dealing with multidimensional data structures like 2D or 3D arrays, matrices, list of lists.
  • Dynamic Programming:Nested loops are commonly used in dynamic Programming for solving problems like knapsack problem or longest common subsequence.

Nested Loops in C
Visit Courseexplore course icon
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp