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

 

Similar post

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

 

Suggested for you

If statement in Java language

If statement in C language

If statement in C++ language

If statement in Python language

Nested-If  in Java language

Nested-If in C++ language

Nested-If  in Python language

 

Electricity bill calculation program Java language

Electricity bill calculation program  Python language

Electricity bill calculation program C language

Electricity bill calculation program C++ language

Electricity bill calculation program using Java method

Electricity bill calculation program using C function

 

 

 

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.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

16 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago