nested for

Nested for loop in C programming language

Nested for loop in C programming language

In this tutorial, we will learn about Nested for loop in C programming language

Already, we discussed for loop in an earlier blog post.

In the C programming language,  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).

Nested for loop in C language

Declaration

The syntax of the for loop

for(initialization; test_expression;increment or decrement  //outer loop          
{
           //statement(s) inside the body          
for(initialization; test_expression;increment or decrement  //inner loop
{
     //statement(s) inside the body
}   //End of inner for loop
}   //End of outer for loop

Structure of nested for loop

Nested for loop

Nested for loop flow diagram

Flow diagram of nested for loop in C

Initially, Nested for loop evaluates outer for loop test expression and evaluates only once.  If the condition is true, the flow of control jumps to the inner for loop.

Then, the loop evaluates the condition of the inner loop. when the condition is true, it executes codes of inside the inner for loop.  If the test expression returns false, the flow of control skips the execution and jumps out to the outer loop for execution.

Then the outer for loop test expression is evaluated again when the test expression is false, the flow of control skips the execution and come out of the loop for rest.

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j;
    for(i=0; i<=2; i++){
    printf("Statement of outer for loop\n");
       for(j=0; j<=3; j++){
            printf("%d %d :",i,j);
    printf("Statement of inner for loop\n");

}
    }
return 0;
}

 

When the above code is compiled and executed, it produces the following result.

Statement of outer for loop
0  0 :Statement of inner for loop
0  1 :Statement of inner for loop
0  2 :Statement of inner for loop
0  3 :Statement of inner for loop
Statement of outer for loop
1  0 :Statement of inner for loop
1  1 :Statement of inner for loop
1  2 :Statement of inner for loop
1  3 :Statement of inner for loop
Statement of outer for loop
2  0 :Statement of inner for loop
2  1 :Statement of inner for loop
2  2 :Statement of inner for loop
2  3 :Statement of inner for loop

 

Example

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b;
    for(a=1; a<=10; a++){
       for(b=1; b<=10; b++){
            printf("%d",b);
}
printf("\n");
    }
    getch();
return 0;
}

 

When the above code compiled and executed, it produced the following result

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910

The above program displays a rectangular number pattern using nested for loop in C

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b;
    for(a=1; a<=10; a++){
       for(b=1; b<=10; b++){
            printf("%c",'*');
}
printf("\n");
    }
    getch();
return 0;
}

 

 

When the above code is compiled and executed, it produces the following result

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

The above program displays rectangular star pattern using nested for loop in C

 

Nested for loop is used to display elements of the array, click here to learn more

 

Suggested for you

For loop in C language

For loop in Java language

for loop in Cpp language

for loop in python language

While loop in Java language

While loop in C language

While loop in Cpp language

While loop in Python language

 

Nested for loop in Python programming language
Nested for loop in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago