Nested Loops in C Language
C programming allows to use one loop inside another loop. The following section shows a few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
The syntax for a nested while loop statement in C programming language is as follows −
while(condition) { while(condition) { statement(s); } statement(s); }
The syntax for a nested do…while loop statement in C programming language is as follows −
do { statement(s); do { statement(s); }while( condition ); }while( condition );
A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a ‘for’ loop can be inside a ‘while’ loop or vice versa.