Java Programming - Flow Control - Discussion

Discussion :: 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);

[A]. i = 1, j = 0
[B]. i = 1, j = 4
[C]. i = 3, j = 4
[D]. Compilation fails.

Answer: Option D

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.


Firat said: (Feb 7, 2012)  
Well, it would be better to ask the output without any missing curly brackets.

Then answer would be i = 0, j = 0, I guess.

Siavash said: (Jul 9, 2012)  
There is no break; in the second eternity loop, which would make the System. Out. Println an unreachable statement.

Jhon said: (Sep 10, 2012)  
There is no main method which is to be noticed.

Mehul said: (Sep 11, 2012)  
If curly brackets would have been there in corrrect pos then answer would be i= 1 and j=0.

Ghogale said: (May 1, 2013)  
No, I run that program it give error unreachable statement at

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

Alex said: (Feb 28, 2014)  
If compilation fails for the lack of the ending curly brace, compilation would fail for the lack of a main function. These questions imply the code snippets are just that - snippets - and so it would be perfectly reasonable to assume there is a curly brace later on in the code but not included in the snippet. That doesn't change the fact that the print statement is unreachable though.

Aravindh said: (Jul 4, 2014)  
What is tp: kindly explain me ?

Pleasebugmenot said: (Aug 15, 2014)  
TP represents as label for the loop.

Vishesh said: (Sep 23, 2014)  
If curly braces of for ended before println then it print i=1 and j=0.

Gaurav said: (Oct 20, 2014)  
How can j = 0, because when break up run it will print the sop statement.

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

Rishi Pathak said: (Nov 26, 2014)  
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.

Yassine said: (Apr 29, 2015)  
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.

Aqu said: (Sep 14, 2015)  
Can anyone explain this source code?

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

Yash Maheshwari said: (Oct 15, 2015)  
If we consider that there are no errors then what could be the result with step by step explanation?

Vikas said: (Dec 2, 2015)  
What is tp doing with break?

Salim Shamim said: (Oct 10, 2016)  
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.

Robin said: (Feb 17, 2017)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.