5.11 The switch Statement
The switch statement can be used instead of an if/else if statement for selection when the condition consists of comparing the value of an expression to constant integers (byte, short, or int), characters (char), or Strings. The syntax of the switch statement is the following:
switch ( expression ) { case constant1: statement1; . . . break; // optional case constant2: statement1; . . . break; // optional . . . default: // optional statement1; . . . }
The expression is first evaluated, then its value is compared to the case constants in order. When a match is found, the statements under that case constant are executed in sequence. The execution of statements continues until either a break statement is encountered ...
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.