Tuesday 15 February 2011

Nested Loops





When working with nested loops, the outer loop changes only after the inner loop is completely finished (or is interrupted.).
//init means initialization
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.

 You may like the following posts: