nested for loop in C Programming language

Given Diagram is the flowchart how for loop works in c programming.
Given flow diagram is for how nested for loops works.The outer for loop i.e first loop start its execution then execute the initialise-for-1 statement which is executed first.
1.initialize-for-1: This is the first section or statments in the for loop. which allows the programmer to declare and initilize the variables those can be used inside test condtion,inside body of loop or in the update expression.
initilize-for-1 executes only once for n iteration of the loop. Next the control jumps to test expression i.e "is condition-for-1 valid?"
2. is condition-for-1 valid?:This is the second statement in the for loop which test the condition.
The test condition may be true or false depends on the validation of test condtion.
When the condition-for-1 evaluates "false" the control can not enters the "second for loop" and stop the entire execution of nested for loop.
When the condition-for-1 evaluates "true" the control Jumps inside the second for loop and start "initilize-for-2".
3.initilize-for-2 this is the first statement inside the inner for loop. The working of this statements is same as the outer for loops "initilize-for-1" statement.
After execution of statement "initilize-for-2" the control jumps to test the condition "is condition-for-2 valid?"
4.is condition-for-2 valid?:This is the second statement in the inner or nested for loop which tests the condition.
The test condition may be true or false depends on the validation of test condtion.
When the condition-for-2 valid? evaluates "true" the control enter inside inner loop and Start executing "Execute statements inside 2nd for loop". These satements is also known as the body of the loop.
After succesful execution of the statements the control start executiong statement "update-for-2".
5. update-for-2:is the update expression or iteration expression in for loop ,it execute after execution of loop body or at the end of each iteration.
This is the one of the important statement in for loop that increments the loop counter variable (update-for-2).
After successful execution of update-for-2 statements the control jumps back to test the condition in step 4: and contiune execution of inner loop till the "is-condition-for-2 valid?". Otherwise control jumps or exit out of inner (second loop) loop and executes the statement "update-for-1".
6.update-for-1: the update expression or iteration expression in outer for loop or first loop,it execute after execution of loop body of first loop or at the end of each iteration.
This is the one of the important statement in for loop that increments the loop counter variable (update-for-2).
After successful execution of update-for-1 statements the control jumps back to test the condition in step 2: and contiune execution of first loop(outer loop) till the "is-condition-for-1 valid?" is true otherwise stop the execution.


nested for loop sysntax in C Programming.

Syntax of Nested for loop in C:

Let us study the syntax of Nested for loop and then we will look detail into all parts of the Nested for loop.

/* statements outside loop. */
for(initilize-for-1;condition-for-1;update-for-1)
  {
     /*second for loop */
     for(initilize-for-2;condition-for-2;update-for-2)
     {
      /*Executes the Statements inside 2nd for loop;*/
     }
  }
/* statement outside loop */


Syntax Explanation :
all statements ouside the for loops executes i.e /*Statements outside loop */
1. Control enters into the outer for loop and initilize and execute the statement initilize-for-1.
2. And The control jumps to test the condition condition-for-1 inside outer for loop.
3. The condition is tested. Here condition tested either be true or false.
3.a. if the condition tested is false, then control do not enters in to inner for loops. it transfer to outside for loop and start executing /* Statements outside the loop.*/
and program stops.
3.b. if the condition condition-for-1 tested is true, then the flow enters inside the loop and executes the statements /* second for loop. */
4.after executing these statements control enters inner for loop.
declare and initilize the statement initilize-for-2.
Then The inner condition i.e. condition-for-2 is tested inside inner for loop.
5.a if tested condition is true then statements inside inner for loop i.e// Executes the Statements inside 2nd for loop.
6.after executing the statements control jumps to update-for-2 values.
After that again condition condition-for-2 is tested , if the tested condition is true that means execution repeats from step no 5.
5.b if tested condition is false control jumps outside the inner for loop and execute update-for-1 and test the condition condition-for-1 . the process repeates till the both conditions are true.


C program to illustrate use of Nested for loop

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


Output:

Table from 1...10 Using Nested for 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 : Variable int n=1,i=1; declared and initilized
printf("\n Table from 1...10 Using Nested for loop");
Shows the message on console
Table from 1...10 Using Nested for loop
Next control enters into the outer for loop initilize n=1 and test the condition n<=10 (1<10) which is true ,The control enters into the body of outer loop .
We have another for loop inside the body of the loop.
The control enters into the inner for loop.
The value i=1 is initilized, next control jumps to test the condition (i<=10) which is true and control enters into the inner for loop body and executes printf("\t %d",n*i); displays output 1 .
Next i++ increase value of i by 1 then i becomes i=2 again control enters and test the condition(i<=10) which is true and execute body of inner loop and shows output 2.
The inner for loop continues till value of i <= 10 .
Once the condition i<=10 becomes false control goes to outer for loop and increase the value of n++ by one.
then n becomes n=2 and the rest of loop iteration continues till value of n<=10.
finaly we get the table output from 1 2 3........10.

Previous Topic:-->> For loop examples in C  || Next topic:-->>Nested for loop Examples in C.