Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

Nested Loops in C ❮ Edit Details

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.

  1. for (initialization; condition; update)   
  2. {  
  3.     for(initialization; condition; update)  
  4.     {  
  5.            // inner loop statements.  
  6.     }  
  7.     // outer loop statements.  
  8. }  

Example of nested for loop

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int n;// variable declaration  
  5.    printf("Enter the value of n :");  
  6.    // Displaying the n tables.  
  7.    for(int i=1;i<=n;i++)  // outer loop  
  8.    {  
  9.        for(int j=1;j<=10;j++)  // inner loop  
  10.        {  
  11.            printf("%d\t",(i*j)); // printing the value.  
  12.        }  
  13.        printf("\n");  
  14.    }  

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 Loops in C

Nested while loop

The nested while loop means any type of loop which is defined inside the 'while' loop.

 

  1. while(condition)  
  2. {  
  3.     while(condition)  
  4.     {  
  5.          // inner loop statements.  
  6.     }  
  7. // outer loop statements.  
  8. }  

Example of nested while loop

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.    int rows;  // variable declaration  
  5.    int columns; // variable declaration  
  6.    int k=1; // variable initialization  
  7.    printf("Enter the number of rows :");  // input the number of rows.  
  8.    scanf("%d",&rows);  
  9.    printf("\nEnter the number of columns :"); // input the number of columns.  
  10.    scanf("%d",&columns);  
  11.       int a[rows][columns]; //2d array declaration  
  12.       int i=1;  
  13.    while(i<=rows) // outer loop  
  14.    {  
  15.        int j=1;  
  16.       while(j<=columns)  // inner loop  
  17.        {  
  18.            printf("%d\t",k);  // printing the value of k.  
  19.            k++;   // increment counter  
  20.            j++;  
  21.        }  
  22.        i++;  
  23.        printf("\n");  
  24.    }  
  25. }  

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 Loops in C

Nested do..while loop

The nested do..while loop means any type of loop which is defined inside the 'do..while' loop.

  1. do  
  2. {  
  3.    do  
  4.   {   
  5.       // inner loop statements.  
  6.    }while(condition);  
  7. // outer loop statements.  
  8. }while(condition);  

Example of nested do..while loop.

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.   /*printing the pattern 
  5.      ******** 
  6.      ******** 
  7.      ******** 
  8.      ******** */  
  9. int i=1;  
  10. do           // outer loop  
  11. {  
  12.     int j=1;  
  13.     do       // inner loop  
  14.    {  
  15.       printf("*");  
  16.       j++;  
  17.    }while(j<=8);  
  18.     printf("\n");  
  19.     i++;  
  20.      }while(i<=4);  
  21. }  

Output:

Nested Loops in C

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.