Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.16)


What will be the output of the program?

for(int i = 0; i < 3; i++) 
        case 0: break; 
        case 1: System.out.print("one "); 
        case 2: System.out.print("two "); 
        case 3: System.out.print("three "); 

[A]. done
[B]. one two done
[C]. one two three done
[D]. one two three two three done

Answer: Option D


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.

Google said: (Apr 18, 2013)  
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: (Sep 4, 2013)  
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: (Sep 30, 2013)  
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: (Dec 12, 2013)  
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: (Jan 4, 2014)  
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: (Nov 26, 2014)  
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.


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");

Ankit said: (Jan 27, 2016)  
In second case when loop execute again then count two three and then three done why?

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

Saikat Roy said: (Sep 29, 2020)  
How can we run this instead of public class? Explain me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.