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).
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
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
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
Suggested for you
If statement 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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…