• 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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!