A nested loop is formed by placing a loop inside the body of another loop. The inner loop is executed to completion for each iteration of the outer loop.
Eventhough nested loops can be formed with any of the loop statement in C, the most commonly used is the for loop. A nested for loop is most commonly used to perform operations on the elements of a multidimensional array.
Here is an example of a nested for loop
#include <stdio.h> #include <conio.h> int main() { int i,j; char ch1='*'; char ch2='O'; for (i=0; i<10; ++i) {//Outer loop starts here printf("\n\t"); for (j=0; j<10; j++) { //Inner loop starts here if (i==j) { printf("%c ",ch2);} else { printf("%c ",ch1);} }//Inner loop ends here } //Outer loop ends here getch(); return 0; }
The output of this program is as below
O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O * * * * * * * * * * O
Nothing yet..be the first to share wisdom.