can anyone clarify me why the following code prints 3,4?
switch (2) {
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
thanks in advance
Ricky Ignatius
Greenhorn
Joined: Oct 09, 2005
Posts: 20
posted
0
switch (2) { case 1: System.out.println(1); case 2: // inside here, no break case 3: // fall throught here, no break System.out.println(3); case 4: // fall throught here, no break System.out.println(4); }
Because the code will go inside case 2 then fall through to case 3, case 4 because they dont have break statement.
Ricky
Gyanesh Sharma
Ranch Hand
Joined: Nov 27, 2005
Posts: 47
posted
0
Because there is no break; statements in your code sample.