Nested Loops in C with Examples
Last Updated : 25 Oct, 2022
A nested loop means a statement inside another loop statement. That is why nested loops are
also called "loop inside loops". We can define any number of loops inside another loop.
1. Nested for Loop
Nested for loop refers to any type of loop that is defined inside a 'for' loop. Below is the equivalent
flow diagram for nested 'for' loops:
Nested for loop in C
Syntax:
for ( initialization; condition;increment) {
for ( initialization; condition; increment) {
// statementof inside loop
// statement of outer loop
Example: Below program uses a nested for loop to print a 3D matrix of 2x3x2.
c
C program to print elements of Three-Dimensional Array
// with the help of nested for loop
#include < stdio.h>
int main ()
// initializing the 3-D array
int
[O, 61, 1, 7), {2, 8]
4, 10 ) , [5, 11
// Printing values 3-D array
for int
for (int j
for (int k
printf( "Element at 7dd\n",
return O ;
Output
Element at
Element at
Element at
Element at
Element at
Element at
Element at
Element at
Element at
Element at = 10
Element
Element at = 11
2. Nested while Loop
A nested while loop refers to any type of loop that is defined inside a 'while' loop. Below is the
equivalent flow diagram for nested 'while' loops:
Nested while loop in C
Syntax:
while(condition) {
while(condition) {
// statementof inside loop
// statement of outer loop
Example: Print Pattern using nested while loops
// C program to print pattern using nested while loops
#include < stdio.h>
int main ( )
int end
printf( 'Pattern Printing using Nested While loop ")
int i
while (i end)
printf( " ) ;
int j
while (j i) [
printf( "Ed
return O ;
Output
Pattern Printing using Nested While loop
12
123
1234
12345
3. Nested do-while Loop
A nested do-while loop refers to any type of loop that is defined inside a do-while loop. Below is
the equivalent flow diagram for nested 'do-while' loops:
do while loop in C
Syntax:
do{
do{
// statementof inside loop
}while(condition) ;
// statement of outer loop
}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any
type of loop nested inside any type and to any level.
Syntax:
do{
while(condition) {
for ( initialization; condition;increment) {
// statementof inside for loop
// statementof inside while loop
// statement of outer do-while loop
}while(condition);
Example: Below program uses a nested for loop to print all prime factors of a number.
C Program to print all prime factors
// of a number using nested loop
Finclude<math.h>
#include < stdio.h>
// A function to print all prime factors of a given number n
void primeFactors (int n)
// Print the number of 2s that divide n
while (n •702
printf(
// n must be odd at this point. So we can skip
// one element (Note i i 42)
for (int 3; i sqrt(n)•
// While i divides n, print i and divide n
while (n % i 0)
printf( "Ed i),
This condition is to handle the case when n
is a prime number greater than 2
printf( "Ed
/* Driver program to test above function */
int main ()
int n 315;
primeFactors(n);
return O ;
Output:
3357
Break Inside Nested Loops
Whenever we use a break statement inside the nested loops it breaks the innermost loop and
program control is inside the outer loop.
Example:
C program to show working of break statement
// inside nested for loops
kinclucle < stdio.h>
int main ()
int 1
for int O; 14
for int j
// This Inner loop will break when i:-3
if (i 3)
break;
printf(
printf( "\n");
return O ,
Output
In the above program, the first loop will iterate from O 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.
Example:
C program to show working of continue statement
// inside nested for loops
kinclucle < stdio.h>
int main ()
int
for (int O; 14
for (int j
// This Inner loop will skip when j=
if (j-
continue
printf( "Ed
printf( "\n");
return O ;
In the above program, the inner loop will be skipped when j will be equal to 2. The outer loop will
remain unaffected.