Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 20)
20.
What will be the output of the program?
int i = 0, j = 5; 
tp: for (;;) 
    {
        i++;  
        for (;;) 
        {
            if(i > --j) 
            {
                break tp; 
            } 
        } 
        System.out.println("i =" + i + ", j = " + j);
i = 1, j = 0
i = 1, j = 4
i = 3, j = 4
Compilation fails.
Answer: Option
Explanation:

If you examine the code carefully you will notice a missing curly bracket at the end of the code, this would cause the code to fail.

Discussion:
17 comments Page 1 of 2.

Robin said:   9 years ago
So guys,

I run the code:

if you add the bracket after the general loop YOU HAVE AN UNREACHABLE statement, wich is logic.

Now, if you put the System.out outside the loop, at the end you will have : i =1, j = 0.

Salim Shamim said:   9 years ago
It will NOT give error of Unreachable statement, decrements of j will cause i==j eventually and break tp; will break the loop with label tp aka the outer loop.

Vikas said:   10 years ago
What is tp doing with break?

Yash Maheshwari said:   10 years ago
If we consider that there are no errors then what could be the result with step by step explanation?

Aqu said:   10 years ago
Can anyone explain this source code?

With proper curly brackets, I get the output i=1 and j=0. How?
(1)

Yassine said:   1 decade ago
The last statement would be unreachable because a for (;;) is equivalent of while (true) although it breaks the TP loop but it doesn't break the loop before the last statement.

Rishi Pathak said:   1 decade ago
Even if you put curly braces "}" at the end, It will give you a "compilation error ".

Because the Print Statement,
System.Out.Println("i =" + I + ", j = " + j); will be unreachable.
(1)

Gaurav said:   1 decade ago
How can j = 0, because when break up run it will print the sop statement.

i.e. i = 1, j = 4.

Vishesh said:   1 decade ago
If curly braces of for ended before println then it print i=1 and j=0.

PleaseBugMeNot said:   1 decade ago
TP represents as label for the loop.


Post your comments here:

Your comments will be displayed after verification.