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);
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.
Alex said:
1 decade ago
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.
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.
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.
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.
Because the Print Statement,
System.Out.Println("i =" + I + ", j = " + j); will be unreachable.
(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.
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.
Ghogale said:
1 decade ago
No, I run that program it give error unreachable statement at
System.out.println("i =" + i + ", j = " + j);
System.out.println("i =" + i + ", j = " + j);
Firat said:
1 decade ago
Well, it would be better to ask the output without any missing curly brackets.
Then answer would be i = 0, j = 0, I guess.
Then answer would be i = 0, j = 0, I guess.
Siavash said:
1 decade ago
There is no break; in the second eternity loop, which would make the System. Out. Println an unreachable statement.
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?
With proper curly brackets, I get the output i=1 and j=0. How?
(1)
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers