Menu
Question Index
...

What will be the output of the following program?

class BreakExample
{
    public static void main(String s[])
    {
        for(int i = 2; i <= 10; i+=2 )
        {
            for( int j = 0; j <= 3; j++, i++ )
            {
                if( i < 8 ) break;

                System.out.println("i = " + i + " j = " + j );
            }
        }

    }
}


i = 8 j = 0
i = 9 j = 1
i = 10 j = 2
i = 11 j = 3
i = 8 j = 0
i = 8 j = 1
i = 8 j = 2
i = 8 j = 3
i = 10 j = 0
i = 10 j = 1
i = 10 j = 2
i = 10 j = 3
Compilation error since we can not give j++ and i++ in the same statement, only one of them can be given.
Compilation error since break can be used only in the outer loop, but here we are using in the inner loop.

Doubts

Problems

Topic: Using Break In for Loop To Exit

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App