Home Segments Index Top Previous Next

467: 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 symbol on one end and a break statement on the other, with a colon separating the anticipated value and the statement sequence:

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

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

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

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