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
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…