C Nested for loop


for loop inside another for loop is known as nested for loop. Nested for loop can contain more than one for loop(two or more)

Syntax:-


for(exp1;exp2;exp3)
{
for(exp1;exp2;exp3)
{
...........
...........
}
}

The first expression : exp 1 contains initialization statements
The second expression: exp 2 contains limit–test expression
The third expression : exp 3 contains updating expression


How Nested for Loop Works

  • Step 1:-
    Hear, there are two Loop first is outer loop and inside outer loop one is inner loop i.e Nested
  • Step 2:-
    first the outer loop variable is initialized and then program control passes to the condition, if the condition is true, then the program control passes to the inner loop. if the condition is false then program control is terminated.
  • Step 3:-
    if the condition is true, the inner loop variable will be initialized and the condition checks whether the condition is true or not if the condition is true then the inner loop statement is executed until the condition is true
  • Step 4:-
    After executed inner loop now control updating the counter variable of outer loop and again the condition is checked. if the condition is true then the inner loop will be executed again.
  • Step 5:-
    This process will continue until the condition of the outer loop is true

Example with dry Run Concepts



steps-shineskill


Note:

The for loop is always used when we know initial value, final value and updating value. The updating value can be constantly incremented or decremented.


Q.) W.A.P to print
123
123
123
using nested for loop three times.
 #include<stdio.h>
 #include<conio.h>
 void main()
 { 
 int i , j;
 for(i = 1; i <=3 ; i++)
 {
 for(j=1;j<=3;j++)
 {
 printf(“%d”, j );
 }
 printf(“\n”); 
 }
 getch();
 }
Output
123
123
123

Explanation of the above Program
  • Step 1:-
    Hear, There are three rows and three columns so,We declared integer variables called i and j, i for rows and j for cols
  • Step 2:-
    first the outer loop variable is initialized i.e i=1 and then program control passes to the condition (i <=3 ) the condition is true, then the program control passes to the inner loop.
  • Step 3:-
    the inner loop variable will be initialized i.e j=1 and the condition checks again for inner loop (j <=3 ) for cols the condition is true then the inner loop statement is executed until the condition is true
  • Step 4:-
    After executed inner loop now control updating the counter variable of outer loop and again the condition is checked. if the condition is true then the inner loop will be executed again.
  • Step 5:-
    This process will continue until the condition of the outer loop is true i.e j <=3


Q.) W.A.P to print
*
**
***
****
*****
using nested for loop
 #include<stdio.h>
 #include<conio.h>
 void main()
 { 
 int i , j;
 for(i = 1; i <=5 ; i++)
 {
 for(j=1;j<=i;j++)
 {
 printf("*");
 }
 printf(“\n”); 
 }
 getch();
 }
Output
*
**
***
****
*****

Explanation of the above Program
  • Step 1:-
    Hear, There are five rows and columns are less than or equals to rows in each rows so,We declared integer variables called i and j, i for rows and j for cols
  • Step 2:-
    first the outer loop variable is initialized i.e i=1 and then program control passes to the condition (i <=5 ) the condition is true, then the program control passes to the inner loop.
  • Step 3:-
    the inner loop variable will be initialized i.e j=1 and the condition checks again for inner loop (j <=i ) for cols the condition is true then the inner loop statement is executed until the condition is true
  • Step 4:-
    After executed inner loop now control updating the counter variable of outer loop and again the condition is checked. if the condition is true then the inner loop will be executed again.
  • Step 5:-
    This process will continue until the condition of the outer loop is true i.e j <=5


Q.) W.A.P to print
*****
****
***
**
*
using nested for loop
 #include<stdio.h>
 #include<conio.h>
 void main()
 { 
 int i , j;
 for(i = 1; i <=5 ; i++)
 {
 for(j=5;j>=i;j--)
 {
 printf("*");
 }
 printf(“\n”); 
 }
 getch();
 }
Output

*****
****
***
**
*

Explanation of the above Program
  • Step 1:-
    Hear, There are five rows and columns are grater than or equals to rows, in each rows so,We declared integer variables called i and j, i for rows and j for cols
  • Step 2:-
    first the outer loop variable is initialized i.e i=1 and then program control passes to the condition (i <=5 ) the condition is true, then the program control passes to the inner loop.
  • Step 3:-
    the inner loop variable will be initialized i.e j=5 and the condition checks again for inner loop (j >=i ) for cols the condition is true then the inner loop statement is executed until the condition is true
  • Step 4:-
    After executed inner loop now control updating the counter variable of outer loop and again the condition is checked. if the condition is true then the inner loop will be executed again.
  • Step 5:-
    This process will continue until the condition of the outer loop is true i.e j >=i