Flowchart of nested while loop in C language

Given diagram shows the flowchart of how a while loop works in C programming.
Initially, the outer while loop evaluates the test expression. If the condition is false, the flow of control skips the loop and comes out of the outer loop. If the condition is true, the control enters the inner while loop.

Inside the inner loop, the test expression is again evaluated. If it’s true, the statements inside the inner loop are executed. If the condition is false, the control skips the inner loop and jumps to the outer loop’s update expression.
After executing the outer loop's statement, the control goes back to the outer while condition and rechecks it. This process continues as long as the condition remains true.


nested while loop syntax C Programming language.

Syntax of Nested while loop in C:

Let us first look at a real-world example of the syntax for a nested while loop in C and then we will go into detail about each part of it.

/* statements outside loop.
initilization */
  while(Outer Condition)
  {
     /*Outer while statements */
     while(Inner Condition)
     {
      /*Inner while Statements; */
     }
     /*Outer While Statements */
  }
/* statement outside loop */


Syntax Explanation:
Before we get into how it works, remember that any statements written outside the loops will execute first — that’s what we mean by /* Statements outside loop */.
1. The program starts by entering the outer while loop.
2. It checks the condition written in the outer while loop.
3. Now two things can happen:
3.a. If the condition is false, the program skips the loop entirely and jumps straight to the statements outside the loop — and ends there.
3.b. If the condition is true, the program enters the loop and starts executing the statements inside the outer while loop — these are your /* Outer while statements */.
4. After the outer loop’s statements run, the program now goes inside the inner while loop.
5. The inner loop condition is checked.
5.a. If true, it runs the statements inside the inner while loop — these are your /* Inner while statements */.
6. Then the control goes back to recheck the inner loop condition. If it’s still true, it keeps running those inner statements again. This process repeats until the inner condition becomes false.
5.b. If the inner condition is false, the program exits the inner while loop and continues with the remaining statements inside the outer loop (if any). Then it moves to step 7.
7. Now it goes back to the top of the outer while loop, rechecks the outer condition, and if it's still true, the whole process repeats from step 2.
In short, the inner loop keeps running until its condition becomes false. Once that happens, the outer loop picks up again — and this keeps going until the outer loop’s condition also turns false. That’s how nested while loops work in a step-by-step flow.


C program to illustrate use of Nested while loop

/* C program given below displays Table from 1 to 10 Number */
#include <stdio.h>
int main()
 {
  int n=1,i=1;
  printf("\n Table from 1...10 Using Nested While loop");
  while(n<=10)
   {
   i=1;
   while(i<=10)
    {
     printf("\t %d",n*i);
     i++;
    }
   n++;
   printf("\n");
   }
  return(0);
 }


Output:

Table from 1...10 Using Nested While loop


1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 26 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100

Explanation:
The variables int n = 1, i = 1; are declared and initialized.
Then, the line printf("\n Table from 1...10 Using Nested While loop"); displays the heading message on the console:
Table from 1...10 Using Nested While loop

Now the program enters the outer loop while(n <= 10).
Since n = 1, the condition is true, so the control moves into the body of the outer loop.

Inside the outer loop, i is re-initialized to 1.
Next, the program enters the inner loop while(i <= 10).
Again, since i = 1, the condition is true and control enters the inner loop’s body.

It then executes printf("\t %d", n * i); which prints the product — in this case, 1 × 1 = 1.
Then i++ increases the value of i to 2.
The condition i <= 10 is checked again, and since it’s still true, the loop continues and prints the next value.

This continues until i becomes 11, which makes the condition false.
At this point, the inner loop ends, and control returns to the outer loop.
Then n++ increases the value of n to 2.
The outer loop checks n <= 10 again, and since it’s true, it goes through the same process for n = 2.

This continues until n becomes 11, which breaks the outer loop.
As a result, we get the full multiplication table from 1 to 10 printed using **nested while loops**.



Other Topics: