CODE
class NestedWhileExample
{
public static void main(String arg[])
{
int outer = 1;
while(outer < 3)
{
int inner = 5;
while(inner < 8)
{
System.out.println(outer + " " + inner);
inner++;
}
outer++;
}
}
}
1 5
1 6
1 7
2 5
2 6
2 7
Here outer loop is executed for values going from 1 till less than 3 i.e. 1 and 2. Similarly inner loop is executed from 5 till less than 8 i.e. 5, 6 and 7. So when outer is 1, inner will become 5, 6, and 7 and when outer is 2, then also inner will become 5, 6, and 7.
inner < 8
to inner < 12
and see what will be output.int outer = 1;
to int outer = -4;
and see the output.while
loop inside the inner while
loop with counter as deepest
. Let deepest
start at 10
and go till 14
and see the output of outer
, inner
and deepest
.