[stmt.switch]

# 8 Statements [[stmt]](./#stmt)

## 8.5 Selection statements [[stmt.select]](stmt.select#stmt.switch)

### 8.5.3 The switch statement [stmt.switch]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L444)

The switch statement causes control to be transferred to one of
several statements depending on the value of a condition[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L448)

If the [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") is an [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]"),
the value of the condition is the value of the [*expression*](expr.comma#nt:expression "7.6.20 Comma operator [expr.comma]");
otherwise, it is the value of the decision variable[.](#2.sentence-1)

The value of the condition shall be of integral type, enumeration type, or class
type[.](#2.sentence-2)

If of class type, the
condition is [contextually implicitly converted](conv#def:contextually_implicitly_converted "7.3 Standard conversions [conv]") to
an integral or enumeration type[.](#2.sentence-3)

If the (possibly converted) type is subject to [integral
promotions](conv.prom "7.3.7 Integral promotions [conv.prom]"), the condition is converted
to the promoted type[.](#2.sentence-4)

Any
statement within the switch statement can be labeled with one or
more case labels as follows:

case [*constant-expression*](expr.const#nt:constant-expression "7.7 Constant expressions [expr.const]") :

where the [*constant-expression*](expr.const#nt:constant-expression "7.7 Constant expressions [expr.const]") shall be
a converted [constant expression](expr.const "7.7 Constant expressions [expr.const]") of the
adjusted type of the switch condition[.](#2.sentence-5)

No two of the case constants in
the same switch shall have the same value after conversion[.](#2.sentence-6)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L471)

There shall be at most one label of the formdefault : within a switch statement[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L479)

Switch statements can be nested; a case or default label
is associated with the smallest switch enclosing it[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L483)

When the switch statement is executed, its condition is
evaluated[.](#5.sentence-1)

If one of the case constants has the same value as the condition,
control is passed to the statement following the matched case label[.](#5.sentence-2)

If
no case constant matches the condition, and if there is adefault label, control passes to the statement labeled by the
default label[.](#5.sentence-3)

If no case matches and if there is no default then none of the statements in the switch is executed[.](#5.sentence-4)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L495)

case and default labels in themselves do not alter the
flow of control, which continues unimpeded across such labels[.](#6.sentence-1)

To exit
from a switch, see break, [[stmt.break]](stmt.break "8.8.2 The break statement")[.](#6.sentence-2)

[*Note [1](#note-1)*:

Usually, the substatement that is the subject of a switch is compound
and case and default labels appear on the top-level
statements contained within the (compound) substatement, but this is not
required[.](#6.sentence-3)

Declarations can appear in the substatement of aswitch statement[.](#6.sentence-4)

â *end note*]

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/statements.tex#L509)

A switch statement of the form

switch ( [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") ) [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")

is equivalent to

{  
 [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]")  
 switch ( [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]") ) [*statement*](stmt.pre#nt:statement "8.1 Preamble [stmt.pre]")  
}

except that the [*init-statement*](stmt.pre#nt:init-statement "8.1 Preamble [stmt.pre]") is in
the same scope as the [*condition*](stmt.pre#nt:condition "8.1 Preamble [stmt.pre]")[.](#7.sentence-1)
