Switch statement

Source: Wikipedia, the free encyclopedia.

In

variable or expression to change the control flow
of program execution via search and map.

Switch statements function somewhat similarly to the if statement used in programming languages like

keywords
as switch, case, select or inspect.

Switch statements come in two main variants: a structured switch, as in Pascal, which takes exactly one branch, and an unstructured switch, as in C, which functions as a type of

compiler optimization
in many cases.

Switch statement in C
switch (age) {
  case 1:  printf("You're one.");            break;
  case 2:  printf("You're two.");            break;
  case 3:  printf("You're three.");
  case 4:  printf("You're three or four.");  break;
  default: printf("You're not 1, 2, 3 or 4!");
}

History

In his 1952 text Introduction to Metamathematics,

Stephen Kleene formally proved that the CASE function (the IF-THEN-ELSE function being its simplest form) is a primitive recursive function
, where he defines the notion definition by cases in the following manner:

"#F. The function φ defined thus
φ(x1 , ... , xn ) =
  • φ1(x1 , ... , xn ) if Q1(x1 , ... , xn ),
  • . . . . . . . . . . . .
  • φm(x1 , ... , xn ) if Qm(x1 , ... , xn ),
  • φm+1(x1 , ... , xn ) otherwise,
where Q1 , ... , Qm are mutually exclusive predicates (or φ(x1 , ... , xn) shall have the value given by the first clause which applies) is primitive recursive in φ1, ..., φm+1, Q1, ..., Qm+1.[3]

Kleene provides a proof of this in terms of the Boolean-like recursive functions "sign-of" sg( ) and "not sign of" ~sg( ) (Kleene 1952:222-223); the first returns 1 if its input is positive and −1 if its input is negative.

Boolos-Burgess-Jeffrey make the additional observation that "definition by cases" must be both

collectively exhaustive
. They too offer a proof of the primitive recursiveness of this function (Boolos-Burgess-Jeffrey 2002:74-75).

The IF-THEN-ELSE is the basis of the

mu-operator
.

The earliest

ALGOL-68
introduced forms of this construct which have survived through modern languages.

Typical syntax

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

  • the first select, followed by an expression which is often referred to as the control expression or control variable of the switch statement
  • subsequent lines defining the actual cases (the values), with corresponding sequences of statements for execution when a match occurs
  • In languages with fallthrough behaviour, a break statement typically follows a case statement to end said statement. [Wells]
  • In some languages, e.g., PL/I, the control expression is optional; if there is no control expression then each alternative begins with a WHEN clause containing a boolean expression and a match occurs for the first case for which that expression evaluates to true. This usage is similar to the if/then/elseif/else structures in some other languages, e.g., Perl.
  • In some languages, e.g., Rexx, no control expression is allowed and each alternative begins with a WHEN clause containing a boolean expression and a match occurs for the first case for which that expression evaluates to true.

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 goto the corresponding sequence of statements. The value (or list/range of values) is usually separated from the corresponding statement sequence by a colon or by 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, otherwise, or else keyword. This executes when none of the other cases match the control expression. In some languages, such as C, if no case matches and the default is omitted the switch statement simply does nothing. In others, like PL/I, an error is raised.

Semantics

Semantically, there are two main forms of switch statements.

The first form are structured switches, as in Pascal, where exactly one branch is taken, and the cases are treated as separate, exclusive blocks. This functions as a generalized if–then–else conditional, here with any number of branches, not just two.

The second form are unstructured switches, as in C, where the cases are treated as labels within a single block, and the switch functions as a generalized goto. This distinction is referred to as the treatment of fallthrough, which is elaborated below.

Fallthrough

In many languages, only the matching block is executed, and then execution continues at the end of the switch statement. These include the Pascal family (Object Pascal, Modula, Oberon, Ada, etc.) as well as PL/I, modern forms of Fortran and BASIC dialects influenced by Pascal, most functional languages, and many others. To allow multiple values to execute the same code (and avoid needing to duplicate code), Pascal-type languages permit any number of values per case, given as a comma-separated list, as a range, or as a combination.

Languages derived from C language, and more generally those influenced by Fortran's

computed GOTO, instead feature fallthrough, where control moves to the matching case, and then execution continues ("falls through") to the statements associated with the next case in the source text. This also allows multiple values to match the same point without any special syntax: they are just listed with empty bodies. Values can be special conditioned with code in the case body. In practice, fallthrough is usually prevented with a break keyword at the end of the matching body, which exits execution of the switch block, but this can cause bugs due to unintentional fallthrough if the programmer forgets to insert the break statement. This is thus seen by many[4] as a language wart, and warned against in some lint tools. Syntactically, the cases are interpreted as labels, not blocks, and the switch and break statements explicitly change control flow. Some languages influenced by C, such as JavaScript, retain default fallthrough, while others remove fallthrough, or only allow it in special circumstances. Notable variations on this in the C-family include C#
, in which all blocks must be terminated with a break or return unless the block is empty (i.e. fallthrough is used as a way to specify multiple values).

In some cases languages provide optional fallthrough. For example, Perl does not fall through by default, but a case may explicitly do so using a continue keyword. This prevents unintentional fallthrough but allows it when desired. Similarly, Bash defaults to not falling through when terminated with ;;, but allows fallthrough[5] with ;& or ;;& instead.

An example of a switch statement that relies on fallthrough is Duff's device.

Compilation

Optimizing compilers such as GCC or Clang may compile a switch statement into either a branch table or a binary search through the values in the cases.[6]
A branch table allows the switch statement to determine with a small, constant number of instructions which branch to execute without having to go through a list of comparisons, while a binary search takes only a logarithmic number of comparisons, measured in the number of cases in the switch statement.

Normally, the only method of finding out if this optimization has occurred is by actually looking at the resultant assembly or machine code output that has been generated by the compiler.

Advantages and disadvantages

In some languages and programming environments, the use of a case or switch statement is considered superior to an equivalent series of if

else if
statements because it is:

  • Easier to debug (e.g. setting breakpoints on code vs. a call table, if the debugger has no conditional breakpoint capability)
  • Easier for a person to read
  • Easier to understand, and consequently easier to maintain
  • Fixed depth: a sequence of "if else if" statements may yield deep nesting, making compilation more difficult (especially in automatically generated code)
  • Easier to verify that all values are handled. Compilers can issue a warning if some enum values are not handled.

Additionally, an

perfect hash
.

In terms of the control-flow graph, a switch statement consists of two nodes (entrance and exit), plus one edge between them for each option. By contrast, a sequence of "if...else if...else if" statements has an additional node for every case other than the first and last, together with a corresponding edge. The resulting control-flow graph for the sequences of "if"s thus has many more nodes and almost twice as many edges, with these not adding any useful information. However, the simple branches in the if statements are individually conceptually easier than the complex branch of a switch statement. In terms of cyclomatic complexity, both of these options increase it by k−1 if given k cases.

Switch expressions

Switch expressions are introduced in Java SE 12, 19 March 2019, as a preview feature. Here a whole switch expression can be used to return a value. There is also a new form of case label, case L-> where the right-hand-side is a single expression. This also prevents fall though and requires that cases are exhaustive. In Java SE 13 the yield statement is introduced, and in Java SE 14 switch expressions become a standard language feature.[8][9][10] For example:

int ndays = switch (month) {
    case JAN, MAR, MAY, JUL, AUG, OCT, DEC -> 31;
    case APR, JUN, SEP, NOV -> 30;
    case FEB -> {
        if (year % 400 == 0) yield 29;
        else if (year % 100 == 0) yield 28;
        else if (year % 4 == 0) yield 29;
        else yield 28; }
};

Alternative uses

Many languages evaluate expressions inside switch blocks at runtime, allowing a number of less obvious uses for the construction. This prohibits certain compiler optimizations, so is more common in dynamic and scripting languages where the enhanced flexibility is more important than the performance overhead.

PHP

For example, in PHP, a constant can be used as the "variable" to check against, and the first case statement which evaluates to that constant will be executed:

switch (true) {
    case ($x == 'hello'):
        foo();
        break;
    case ($z == 'howdy'): break;
}
switch (5) {
    case $x: break;
    case $y: break;
}

This feature is also useful for checking multiple variables against one value rather than one variable against many values. COBOL also supports this form (and others forms) in the EVALUATE statement. PL/I has an alternative form of the SELECT statement where the control expression is omitted altogether and the first WHEN that evaluates to true is executed.

Ruby

In Ruby, due to its handling of === equality, the statement can be used to test for variable’s class:

case input
when Array then puts 'input is an Array!'
when Hash then puts 'input is a Hash!'
end

Ruby also returns a value that can be assigned to a variable, and doesn’t actually require the case to have any parameters (acting a bit like an else if statement):

catfood =
  case
  when cat.age <= 1
    junior
  when cat.age > 10
    senior
  else
    normal
  end

Assembler

A switch statement in assembly language:

switch:
  cmp ah, 00h
  je a
  cmp ah, 01h
  je b
  jmp swtend   ; No cases match or "default" code here
a:
  push ah
  mov al, 'a'
  mov ah, 0Eh
  mov bh, 00h
  int 10h
  pop ah
  jmp swtend   ; Equivalent to "break"
b:
  push ah
  mov al, 'b'
  mov ah, 0Eh
  mov bh, 00h
  int 10h
  pop ah
  jmp swtend   ; Equivalent to "break"
  ...
swtend:

Python

For Python 3.10.6,

PEPs 634-636 were accepted, which added match and case keywords.[11][12][13][14]
Unlike other languages, Python notably doesn't exhibit fallthrough behavior.

letter = input("Put in a single letter: ").strip()[0].casefold() # first non-whitespace character of the input, lowercase
match letter:
    case 'a' | 'e' | 'i' | 'o' | 'u': # Unlike conditions in if statements, the `or` keyword cannot be used here to differentiate between cases
        print(f"Letter {letter} is a vowel!")
    case 'y':
        print(f"Letter {letter} may be a vowel.")
    case _: # `case _` is equivalent to `default` from C and others
        print(f"Letter {letter} is not a vowel!")

Exception handling

A number of languages implement a form of switch statement in

Visual Basic .NET
.

Alternatives

Some alternatives to switch statements can be:

(In some languages, only actual data types are allowed as values in the lookup table. In other languages, it is also possible to assign
functions as lookup table values, gaining the same flexibility as a real switch statement. See Control table
article for more detail on this).
Lua does not support case/switch statements.[15] This lookup technique is one way to implement switch statements in the Lua language, which has no built-in switch.[15]
In some cases, lookup tables are more efficient than non-
binary search lookup, however, will almost certainly be slower than either a non-optimized switch or the equivalent multiple if-else statements.[citation needed
]
  • A control table (that may be implemented as a simple lookup table) can also be customized to accommodate multiple conditions on multiple inputs if required and usually exhibits greater 'visual compactness' than an equivalent switch (that can occupy many statements).
  • Pattern matching, which is used to implement switch-like functionality in many functional languages.

See also

References

  1. .
  2. .
  3. ^ "Definition by cases", Kleene 1952:229
  4. .
  5. ^ since version 4.0, released in 2009.
  6. ^ Vlad Lazarenko. From Switch Statement Down to Machine Code
  7. .
  8. ^ "JEP 325: Switch Expressions (Preview)". openjdk.java.net. Retrieved 2021-04-28.
  9. ^ "JEP 354: Switch Expressions (Second Preview)". openjdk.java.net. Retrieved 2021-04-28.
  10. ^ "JEP 361: Switch Expressions". openjdk.java.net. Retrieved 2021-04-28.
  11. ^ Galindo Salgado, Pablo. "What's New In Python 3.10". Python 3.10.6 documentation. Retrieved 2022-08-19.
  12. ^ Bucher, Brandt; van Rossum, Guido (2020-09-12). "PEP 634 – Structural Pattern Matching: Specification". Python Enhancement Proposals. Retrieved 2022-08-19.
  13. ^ Kohn, Tobias; van Rossum, Guido (2020-09-12). "PEP 635 – Structural Pattern Matching: Motivation and Rationale". Python Enhancement Proposals. Retrieved 2022-08-19.
  14. ^ Moisset, Daniel F. "PEP 636 – Structural Pattern Matching: Tutorial". Python Enhancement Proposals. Retrieved 2022-08-19.
  15. ^ a b Switch statement in Lua

Further reading