Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.10)


What will be the output of the program?

int I = 0;
    while (true) 
        for (int j = 0; j < 10; j++) 
            I += j;
            if (j == 3)
                continue inner;
            break outer;
        continue outer;

[A]. 1
[B]. 2
[C]. 3
[D]. 4

Answer: Option A


The program flows as follows: I will be incremented after the while loop is entered, then I will be incremented (by zero) when the for loop is entered. The if statement evaluates to false, and the continue statement is never reached. The break statement tells the JVM to break out of the outer loop, at which point I is printed and the fragment is done.

Siva said: (Aug 31, 2011)  
Then I think output is 0. , but 1 is here. How. ?

Please explain it.

Dinesh said: (Sep 27, 2011)  
Hi Everyone, Could anyone tell me how the output is 1, here? Since there is a label outside, how the increment could happen without calling it?

Sahil said: (Mar 10, 2012)  
Initially I is 0
Then we enter in outer label which includes while loop.
Since while is true so I is incremented and now I=1.
Then inner label starts which includes for loop.
Since j==0 so if condition comes out to be false and continue inner never reach.
Then next statement break outer is executed. which ends while loop too, as it was on it.

Then Through System.out.print Value of I is printed which is 1

Hope you understand...

Rajeshwari said: (Jul 27, 2014)  
But they said "continue Outer" won't the control goes to it again?

Arnold Villasanta said: (Aug 10, 2014)  

"continue Outer" because "break Outer" was called already.

Rishi Pathak said: (Nov 26, 2014)  
int I = 0; // Initialized to zero so, I=0.


while (true) // Enter the while as while is true.
I++; Value of I is incremented, So I=1.


for (int j = 0; j < 10; j++) // J is less than 10 is for loop is executed.
I += j; // I = I+J, so I = 1 + 0, So I = 1 now.
if (j == 3) // Gives False, so Continue inner does not reached, Note Single line so no need to give {} after continue inner.
continue inner; // This is NOT executed.
break outer; // This is executed, which breaks outer as well as inner.

continue outer;// not reached.
System.out.println(I); // I value which remains as 1 is printed.

Savika Singh said: (Mar 10, 2015)  
I like these types of questions according to me each and every student should refer to these questions.

Tejareddy said: (Feb 3, 2018)  
I think so, I is 0 and j is1 i ++ means if the i=0 then 1+0 it becomes the value keeps on adding many times until you get ++i then it adds 1+1..

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.