Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 16)
16.
What will be the output of the program?
for(int i = 0; i < 3; i++) 
{ 
    switch(i) 
    { 
        case 0: break; 
        case 1: System.out.print("one "); 
        case 2: System.out.print("two "); 
        case 3: System.out.print("three "); 
    } 
} 
System.out.println("done");
done
one two done
one two three done
one two three two three done
Answer: Option
Explanation:

The variable i will have the values 0, 1 and 2.

When i is 0, nothing will be printed because of the break in case 0.

When i is 1, "one two three" will be output because case 1, case 2 and case 3 will be executed (they don't have break statements).

When i is 2, "two three" will be output because case 2 and case 3 will be executed (again no break statements).

Finally, when the for loop finishes "done" will be output.

Discussion:
9 comments Page 1 of 1.

Google said:   1 decade ago
But in 1st case when i=0 because of break statement it goes to System.out.println("done"); can you explain why its not happened?

Yeshvanth said:   1 decade ago
When i=0 for loop not ends it will continue the iteration so it won't print "done" after case 0.

Somegowda H M said:   1 decade ago
In java when the control reach the break statement then it not only goes out of the switch but also goes out of the loop itself. So the output should be "done".

Connel Hooley said:   1 decade ago
Surely if i = 1 it won't run all three cases? I know it doesn't have break statements but it wouldn't just ignore the case test on the next line would it?

Vivek said:   1 decade ago
Hi guys the break statement only breaks inner loop switch but not outer loop for. The case statements 1, 2, 3 also does not has break, so if we choose 1 in switch case it will execute all 3 cases. So only its showing output when i=1 as one two three and when i=2 as two three.

Rishi Pathak said:   1 decade ago
Switch(i) = 0 --> goesTo ,--> case 0: break;---> Since Break Present comes out of switch.

NOTE---- ITS WRONG ASSUMPTION THAT IN JAVA Break, will force it self out of FOR loop.

NOW,

Switch(i) = 1 ---> goesTO-->

Case 1: System.out.print("one ");

Note Since NO break, IT executes the code below, which are:

Case 2: System.out.print("two ");

Case 3: System.out.print("three ");

Switch(i) = 2 ---> goesTO--> case 2: System.out.print("two ");

Note Since NO break, IT executes the code below, which is:

Case 3: System.out.print("three ");

For LOOP Condition Fails Since (i < 3; FAILS) as i = 3 so, Control is out of For LOOP and and goesTo-->System.out.println("done");
(1)

Ankit said:   10 years ago
In second case when loop execute again then count two three and then three done why?

Anonymmous. said:   7 years ago
Shouldn't the last statement say System.out.print("done") because println prints on the next line making all options wrong.

Saikat Roy said:   5 years ago
How can we run this instead of public class? Explain me.

Post your comments here:

Your comments will be displayed after verification.