Switch Statement - Typical Syntax

Typical Syntax

In most languages, a switch statement is defined across many individual lines using one or two keywords. A typical syntax is:

  • The first line contains the basic keyword, usually switch, case or select, followed by an expression which is often referred to as the control expression or control variable of the switch statement.
  • Subsequent lines define the actual cases (the values) with corresponding sequences of statements that should be executed when a match occurs.

Each alternative begins with the particular value, or list of values (see below), that the control variable may match and which will cause the control to go to the corresponding sequence of statements. The value (or list/range of values) is usually separated from the corresponding statement sequence by a colon or an implication arrow. In many languages, every case must also be preceded by a keyword such as case or when. An optional default case is typically also allowed, specified by a default or else keyword; this is executed when none of the other cases matches the control expression.

Read more about this topic:  Switch Statement

Famous quotes containing the word typical:

    It is indeed typical that you Earth people refuse to believe in the superiority of any world but your own. Children looking into a magnifying glass, imagining the image you see is the image of your true size.
    —Franklin Coen. Joseph Newman. The Monitor (Douglas Spencer)