Nested Loops
 

The placing of one loop inside the body of another loop is called nesting. When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops may be nested, the most commonly nested loops are for loops.

nested loops


counters

your car's odometer are little more than seven or eight nested for loops, each going from 0 to 9. The far-right number iterates the fastest, visibly moving from 0 to 9 as you drive your car .

The far-right number, however, is not the only number that is moving. All of the other numbers are moving also, but at a much slower pace. For every 10 numbers that move in the column on the right, the adjacent column is incremented by one.

 



for(num2 = 0; num2 <= 3; num2++)
{
           for(num1 = 0; num1 <= 2; num1++)
             {
              System.out.println(num2 + " " + num1);
             }
}


When working with nested loops, the outer loop changes only after the inner loop is completely finished

(c) Shilpa Sayura Foundation 2006-2017