Menu
Topics Index
...
`


Control Statements > Iteration statements (Loops) >
Siva Nookala - 11 Apr 2016
Placing one while loop with in the body of another while is called Nested while loop in java programm. Similar to nested loop.

Nesting while, do-while will work similar to Nested for Loop In Java.
Nested while example
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++;
        }
    
    }
}
OUTPUT

1 5
1 6
1 7
2 5
2 6
2 7

DESCRIPTION

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.

THINGS TO TRY
  • Change the condition inner < 8 to inner < 12 and see what will be output.
  • Change the initialization of int outer = 1; to int outer = -4; and see the output.
  • Try including one more 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.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App