Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
int card = 11;

switch (card) {
  /* ... */
  case 11: 
    System.out.println("Jack");
  case 12: 
    System.out.println("Queen"); 
    break;
  case 13: 
    System.out.println("King"); 
    break;
  default: 
    System.out.println("Invalid Card"); 
    break;
}

Compliant Solution

In this This compliant solution , terminates each case (including the default case) is terminated by a break statement.

Code Block
bgColor#CCCCFF
int card = 11;

switch (card) {
  /* ... */
  case 11: 
    System.out.println("Jack");
    break;
  case 12: 
    System.out.println("Queen"); 
    break;
  case 13: 
    System.out.println("King"); 
    break;
  default: 
    System.out.println("Invalid Card"); 
    break;
}

...

Code Block
bgColor#CCCCFF
int card=11;
int value;

//* CaseCases 11,12,13 fall through to the same case */
switch (card) {
  //* MSC13-J:EX2: these three cases are treated identically */
  case 11: 
  case 12: 
  case 13: 
    value=10; 
  break;
  default: 
    /*/ Handle Error Condition */ 
}

EX3: A case needs no break statement if its last statement is a return or throw.

...