In this video tutorial we’ll demonstrate the use of nested for loop in C programming language.
Related Read:
For Loop In C Programming Language
Nested While Loop: C Program
Note: For every single iteration of the outer while loop, the inner while loop completes its iterations.
A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.
Total Number of Iterations In Nested Loops
Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.
Video Tutorial: Nested For Loop In C Programming Language
[youtube https://www.youtube.com/watch?v=mAV6PW-snmI]
Source Code: Nested For Loop In C Programming Language
#include<stdio.h> int main() { int i, j; for(i = 1; i <= 3; i++) { printf("\n%d Apple\n", i); for(j = 1; j <= 2; j++) { printf("\t%d Oracle\n", j); } } return 0; }
Output:
1 Apple 1 Oracle 2 Oracle 2 Apple 1 Oracle 2 Oracle 3 Apple 1 Oracle 2 Oracle
Explanation of above C source code
Here when i value is 1 – in first iteration. j value gets initialized to 1. And inner for loop executes until j value is equal to 2. For second iteration of outer for loop i value is 2, now again j value gets initialized to 1. Now the inner for loop executes until j value is equal to 2. For third iteration of outer for loop i value is 3, now again j value gets initialized to 1. Now again the inner for loop executes until j value is equal to 2.
Source Code: Nested For Loop In C Programming Language
#include<stdio.h> int main() { int i, j; for(i = 1; i <= 3; i++) { printf("\n%d Apple\n", i); for(j = 1; j <= i; j++) { printf("\t%d Oracle\n", j); } } return 0; }
Output:
1 Apple 1 Oracle 2 Apple 1 Oracle 2 Oracle 3 Apple 1 Oracle 2 Oracle 3 Oracle
Explanation of above C source code
During first iteration of outer for loop, i value will be 1. Now j gets initialized to 1 and inner for loop iterates until j is equal to the value of i(that is 1). Now for second iteration of outer for loop i value is 2. Again j gets initialized to 1 and inner for loop executes until j value is equal to i. and so on ..
Source Code: Nested For Loop In C Programming Language
#include<stdio.h> int main() { int i, j, k; for(i = 1; i <= 5; i++) { printf("\n%d Apple\n", i); for(j = 1; j <= 3; j++) { printf("\t%d Oracle\n", j); for(k = 1; k <= 2; k++) { printf("\t\t%d IBM\n", k); } } } return 0; }
Output:
1 Apple 1 Oracle 1 IBM 2 IBM 2 Oracle 1 IBM 2 IBM 3 Oracle 1 IBM 2 IBM 2 Apple 1 Oracle 1 IBM 2 IBM 2 Oracle 1 IBM 2 IBM 3 Oracle 1 IBM 2 IBM 3 Apple 1 Oracle 1 IBM 2 IBM 2 Oracle 1 IBM 2 IBM 3 Oracle 1 IBM 2 IBM 4 Apple 1 Oracle 1 IBM 2 IBM 2 Oracle 1 IBM 2 IBM 3 Oracle 1 IBM 2 IBM 5 Apple 1 Oracle 1 IBM 2 IBM 2 Oracle 1 IBM 2 IBM 3 Oracle 1 IBM 2 IBM
Note: We can have while loop inside a for loop or a for loop inside a while loop. This is also called as nested looping.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert