Menu
Question Index
...

Compare the following programs WhileExample1 and WhileExample2

class WhileExample1
{
    public static void main(String s[])
    {
        int n = 0;

        while( n <= 6 )
        {
            System.out.println("n = " + n);
            n++;
        }

    }
}

class WhileExample2
{
    public static void main(String s[])
    {
        int n = 0;

        while(true)
        {

            if( n > 6 ) break;
            System.out.println("n = " + n);
            n++;
        }

    }
}


Both WhileExample1 and WhileExample2 are the same and generate same output.
The last line in the output of WhileExample1 will be n = 5.
WhileExample2 goes into a infinite loop.
None of the above.

Doubts

Problems

Topic: Java for loops vs Java while loops vs Java do while loops

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App