CODE
class NestedForLoopExample
{
public static void main(String arg[])
{
for(int outer = 0; outer <= 2; outer++)
{
for(int inner = 0; inner <= 3; inner++)
{
System.out.println(outer + " " + inner);
}
System.out.println();
}
}
}
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
In the above example outer
and inner
are initialized to zero. For every increment of outer
(i.e outer = 0, 1, 2) loop, the inner loop executes four times (i.e inner
= 0, 1, 2, 3). So, we get the output as shown above. Note that the outer loop's condition is outer <= 2
where as for inner loop it is inner <= 3
.
outer <= 2;
to outer <= 5;
and see the output.inner <= 3;
to inner <= 1;
and see the output.deepest
and print the values outer
, inner
and deepest
.