Menu
Topics Index
...
`

Using Break keyword to exit nested loops


If you need explanation Read this topic

If you need Answer Take test on this topic

Using Break keyword to exit nested loops
What is the output of the following program?
public class LoopTest {
    public static void main(String[] args) {
        int x = 0;
        out : for (int i = 0; i <= 5; i++) {
            for (int j = 0; j <= 49; j++) {
                if (i == 2)
                break out;
                x++;
            }
        }
        System.out.println("x = " + x);
    }
}

A. x = 5
B. x = 49
C. x = 99
D. x = 100
Topic: Using break In Nested Loop Java Program

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D 100 is the correct answer after traceing this loops

Posted by Ram Reddy Bitla    2013-08-15 03:33:08


answer:D, for both i=0 and i=1 ,the out label executes 2 times and when i=2,the loop breaks,hence the value of x is 100

Posted by Krishna Chaitanya    2013-08-15 03:35:21


Answer is D .since outer loop executes 2 times and inner loop executes 50 times each.. and x increments to 100 before outer loop breaks at i=2

Posted by Shiva Kaushik    2013-08-15 03:41:24


D

Posted by Gopinath Manchikanti    2013-08-15 05:24:04


Congratulations Shiva Kaushik. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-08-16 03:23:19


Thanks @meritcampus

Posted by Shiva Kaushik    2013-08-18 14:51:57


Congrats kaushk garu..

Posted by Vinod Kumar M    2013-08-18 14:58:39


yaaaaaaaaaaaaaa....................party

Posted by Krishna Saradhi    2013-08-22 15:40:29


© meritcampus 2019

All Rights Reserved.

Open In App