Nested Loops in C Language

Nested Loops in C Language

C programming language supports the concept of nesting loops, allowing you to loop through statements within anoth

er loop. This feature provides flexibility in controlling program flow. Let’s explore examples of nesting loops in C.

You can define any number of loops inside another loop without any restrictions. The nesting level can be defined at any number of times. You can even use different types of loops inside each other; for instance, you can place a ‘while’ loop inside a ‘for’ loop.

Syntax of Nested Loops:

Outer_loop  
{  
    Inner_loop  
   {  
         // Inner loop statements.  
   }  
       // Outer loop statements.  
}

Both Outer_loop and Inner_loop can be valid loops, such as ‘for’ loops, ‘while’ loops, or ‘do-while‘ loops.

Nested for loop:

A nested for loop is any type of loop defined within another ‘for’ loop.

for (initialization; condition; update)   
{  
    for(initialization; condition; update)  
    {  
           // Inner loop statements.  
    }  
    // Outer loop statements.  
}

Example of a Nested for loop:

#include <stdio.h>  
int main()  
{  
   int n; // Variable declaration  
   printf("Enter the value of n :");  
   scanf("%d", &n); // Input for n  

   // Displaying the multiplication tables.  
   for(int i = 1; i <= n; i++) // Outer loop  
   {  
       for(int j = 1; j <= 10; j++) // Inner loop  
       {  
           printf("%d\t", (i * j)); // Printing the value.  
       }  
       printf("\n");  
   }  
}

Explanation of the above code:

  1. The ‘i’ variable is initialized to 1.
  2. Program control checks the condition ‘i <= n.’
  3. If the condition is true, the program control enters the inner loop.
  4. The inner loop executes until the condition is no longer true.
  5. After the inner loop, the control moves to the outer loop’s update part (i.e., i++).
  6. The value of the loop counter ‘i’ is incremented, and the condition ‘i <= n’ is checked again.
  7. If the condition is still true, the inner loop executes again.
  8. This process continues until the condition of the outer loop is no longer true.

Output:

Nested Loops in C

Nested while loop:

A nested while loop is any type of loop defined inside another ‘while’ loop.

while (condition)  
{  
    while (condition)  
    {  
         // Inner loop statements.  
    }  
    // Outer loop statements.  
}

Example of a 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("%d\t", k); // Printing the value of k.  
           k++; // Increment counter  
           j++;  
       }  
       i++;  
       printf("\n");  
   }  
}

Explanation of the above code:

  1. We create a 2D array, int a[rows][columns].
  2. The program initializes the ‘i’ variable to 1.
  3. Control enters the outer loop, which checks the condition. If true, it moves to the inner loop.
  4. After the inner loop execution, the control updates ‘i’ (i.e., i++) and checks the outer loop condition.
  5. If the outer loop condition is true, the control re-enters the inner loop.
  6. This process continues until the outer loop condition becomes false.

Output:

Nested Loops in C

Nested do..while loop:

A nested do..while loop is any type of loop defined inside another ‘do..while’ loop.

do  
{  
   do  
   {   
      // Inner loop statements.  
   } while (condition);  
// Outer loop statements.  
} while (condition);  

Example of a 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);  
}

Explanation of the above code:

  1. We initialize the outer loop counter variable ‘i’ to 1.
  2. The do..while loop executes once without checking the condition, allowing the inner loop to execute.
  3. After the inner loop, ‘i’ is incremented.
  4. When ‘i’ is incremented, the outer loop’s condition (i <= 4) is checked.
  5. If the condition is true, the inner loop is executed again.
  6. This process repeats until the outer loop’s condition is no longer true.

Output:

Nested Loops in C


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading