Menu
Topics Index
...
`

While output


If you need explanation Read this topic

If you need Answer Take test on this topic

While output
What will be the output of the program?
class Output
{
    public static void main(String [] args)
    {
        int I = 0;
        outer:
            while (true)
            {
                I++;
                inner:
                    for (int i = 0; i < 10; i++)
                    {
                        I += i;
            
                        if (i == 1 || i == 2 || i == 3)
                            continue inner;
                        break outer;
                    }
                    continue outer;
            }
            System.out.println("I = " + I);
    }
}

A. I = 1
B. I = 2
C. I = 3
D. I = 0
E. Compilation error
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.

Ans : a.
i.e, I=1.
initially ,I is initialized to 0. and two labels are
created forjump to that line(inner and outter).
while runs forever....
in while  I is incremented so I=1.
in the for loop I is incremented with i i.e, I+=i , here I=1 because i=0.
now if condition fails
executes break statement so it come out of outter.
and executes printf statement and prints I=1.

Posted by Ananthoju Arun Chary    2015-01-06 10:53:44


ans is A as initially I=0 outer and inner are labels declared here precondition loop while with condition true I is incremented first ie I=1 and in inner for(int i=0;i<10;i++) in the loop I+=i is I=I+i  here i=0 then I=1..  if condition  is failed  it jumps to outer and it prints I=1...therefore output is I=1

Posted by Ramya Reddy Addulla    2015-01-06 12:28:19


ans is A as I=0 outer and inner are labels ..

Posted by Shubham Bansal    2015-01-06 19:57:20


a... here while loop.,for lopp, break continue are used wich are described as - while -it is a entry control loop... thewhileloop checks the condition/expression before the block is executed, the control structure is often also knownas a. pre-test loop........break -> it causes the control to go out of loop and stop iteration.... continue -> it causes contrl to nove to next iteration without executing remaining codes of loop.. here two labels outer for while loop and inner for " for" loop are used.. initialy I=0 inside while loop I =1 ...den inside for loop I + (plus)=i hence I =1 now as i(small) = 0 ...if cndtn fails hence with breAk it jumps to oyter loopand prints I=1 and therefore outpt is 1

Posted by Asma Mujtaba Khan    2015-01-07 04:44:19


Ans is A..here we have the Labels concept in the loops which are used to break or continue for particular loop from the other loops also...if we write normal break than it will terminate the current loop..but using labels we can terminate other loops also..

here we have 2 loops named with outer and inner
here we have I=0 than it will enter into loop and incremented ot 1 than it will enters into for loop than i will added which is 0 next we are checking but condition is false so it break the outer loop after coming out of the loop I is 1

Posted by Uday Kumar    2015-01-07 16:56:40


This dose is now closed and the winners are Ananthoju Arun Chary,  for 'First Correct Comment', Ananthoju Arun Chary, Ramya Reddy Addulla,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-09 02:16:13


© meritcampus 2019

All Rights Reserved.

Open In App