Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - Finding the output (Q.No. 10)
10.
What will be the output of the program?
int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
Answer: Option
Explanation:
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.
Discussion:
8 comments Page 1 of 1.
Tejareddy said:
8 years ago
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..
Savika singh said:
1 decade ago
I like these types of questions according to me each and every student should refer to these questions.
Rishi Pathak said:
1 decade ago
int I = 0; // Initialized to zero so, I=0.
Outer:
while (true) // Enter the while as while is true.
{
I++; Value of I is incremented, So I=1.
Inner:
for (int j = 0; j < 10; j++) // J is less than 10 is true.so 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.
Outer:
while (true) // Enter the while as while is true.
{
I++; Value of I is incremented, So I=1.
Inner:
for (int j = 0; j < 10; j++) // J is less than 10 is true.so 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.
(1)
Arnold villasanta said:
1 decade ago
@Rajseshwari:
"continue Outer" because "break Outer" was called already.
"continue Outer" because "break Outer" was called already.
Rajeshwari said:
1 decade ago
But they said "continue Outer" won't the control goes to it again?
Sahil said:
1 decade ago
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...
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...
Dinesh said:
1 decade ago
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?
Siva said:
1 decade ago
Then I think output is 0. , but 1 is here. How. ?
Please explain it.
Please explain it.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers