Home Segments Top Top Previous Next

463: Mainline

The purpose of a switch statement is to execute a particular sequence of statements according to the value of an expression that produces an integer. In most switch statements, each anticipated value of the integer-producing expression and the corresponding sequence of statements is sandwiched between a case keyword on one end and a break or return statement on the other, with a colon separating the anticipated value and the statement sequence:

switch (integer-producing expression) { 
  case integer literal 1: statements for integer 1 break; 
  case integer literal 2: statements for integer 2 break; 
  ... 
  default: default statements 
} 

When such a switch statement is encountered, the integer-producing expression is evaluated, producing an integer. That value is compared with the integer literals found following the case keywords. As soon as there is a match, evaluation of the following statements begins; execution continues up to the first break or return statement encountered.

The line beginning with the default: keyword is optional. If the expression produces an integer that fails to match any of the case integer literals, the statements following the default: keyword are executed.

If there is no match and no default: keyword, no statements are executed.