up previous next contents
Next: while and do-while Up: Flow control and loops Previous: if-else   Contents

switch

  • Evaluates an integer expression in the switch statement.
  • Transfers control to an appropriate case label.
  • If no match is found, control is transferred to a default label.
  • If there is no default label and no other match is found, the switch statement is skipped.
  • A break statement is usually used at the end of each case. If a break is not used, control flow falls through to the next case.
  • All case labels must be constant expressions.
  • The value that you are switching on must be byte, short, char, or int.
  • Example:
      switch (verbosity)
      {
         case BLATHERING:
            System.out.println("blah blah blah ... my name is ...");
         case NORMAL:
            System.out.println("What is your name?");
         case TERSE:
            System.out.println("Yo.");
            break;
         default:
            System.out.println("Hello.");
      }
    


up previous next contents
Next: while and do-while Up: Flow control and loops Previous: if-else   Contents