Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch?

 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there is no break; statements in your code sample.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic