Case statement in cpp

switch statement

Transfers control to one of several statements, depending on the value of a condition.

Contents

[edit] Syntax

  • an expression statement (which may be a null statement » ; «)
  • a simple declaration, typically a declaration of a variable with initializer, but it may declare arbitrarily many variables or structured bindings (since C++17)
  • an alias declaration
  • an expression, in this case the value of condition is the value of the expression
  • a declaration of a single non-array variable of such type with a brace-or-equals initializer, in this case the value of condition is the value of the declared variable

The value of condition must be of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type. If the (possibly converted) type is subject to integral promotions, condition is converted to the promoted type.

[edit] Explanation

The body of a switch statement may have an arbitrary number of case: labels, as long as the values of all constant-expressions are unique (after conversions/promotions). At most one default: label may be present (although nested switch statements may use their own default: labels or have case: labels whose constants are identical to the ones used in the enclosing switch).

If condition evaluates to a value that is equal to the value of one of constant-expression s, then control is transferred to the statement that is labeled with that constant-expression .

Читайте также:  Javascript document body contenteditable true document designmode on void 0

If condition evaluates to a value that doesn’t match any of the case: labels, and the default: label is present, control is transferred to the statement labeled with the default: label.

If condition evaluates to a value that doesn’t match any of the case: labels, and the default: label is not present, then none of the statements in the switch body is executed.

The break statement, when encountered in statement exits the switch statement:

switch (1) { case 1: std::cout  '1'; // prints "1", case 2: std::cout  '2'; // then prints "2" }
switch (1) { case 1: std::cout  '1'; // prints "1" break; // and exits the switch case 2: std::cout  '2'; break; }

Compilers may issue warnings on fallthrough (reaching the next case label without a break) unless the attribute [[fallthrough]] appears immediately before the case label to indicate that the fallthrough is intentional.

If init-statement is used, the switch statement is equivalent to


init-statement
switch ( condition ) statement

Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of statement .

Because transfer of control is not permitted to enter the scope of a variable, if a declaration statement is encountered inside the statement , it has to be scoped in its own compound statement:

switch (1) { case 1: int x = 0; // initialization std::cout  x  '\n'; break; default: // compilation error: jump to default: // would enter the scope of 'x' without initializing it std::cout  "default\n"; break; }
switch (1) { case 1: { int x = 0; std::cout  x  '\n'; break; } // scope of 'x' ends here default: std::cout  "default\n"; // no error break; }

[edit] Keywords

[edit] Example

The following code shows several usage cases of the switch statement

#include int main() { const int i = 2; switch (i) { case 1: std::cout  '1'; case 2: // execution starts at this case label std::cout  '2'; case 3: std::cout  '3'; [[fallthrough]]; // C++17 attribute to silent the warning on fallthrough case 5: std::cout  "45"; break; // execution of subsequent statements is terminated case 6: std::cout  '6'; } std::cout  '\n'; switch (i) { case 4: std::cout  'a'; default: std::cout  'd'; // there are no applicable constant expressions // therefore default is executed } std::cout  '\n'; switch (i) { case 4: std::cout  'a'; // nothing is executed } // when enumerations are used in a switch statement, many compilers // issue warnings if one of the enumerators is not handled enum color { RED, GREEN, BLUE }; switch (RED) { case RED: std::cout  "red\n"; break; case GREEN: std::cout  "green\n"; break; case BLUE: std::cout  "blue\n"; break; } // the C++17 init-statement syntax can be helpful when there is // no implicit conversion to integral or enumeration type struct Device { enum State { SLEEP, READY, BAD }; auto state() const { return m_state; } /*. */ private: State m_state{}; }; switch (auto dev = Device{}; dev.state()) { case Device::SLEEP: /*. */ break; case Device::READY: /*. */ break; case Device::BAD: /*. */ break; } // pathological examples // the statement doesn't have to be a compound statement switch (0) std::cout  "this does nothing\n"; // labels don't require a compound statement either switch (int n = 1) { case 0: case 1: std::cout  n  '\n'; } }

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1767 C++98 condition s of types that are not subject to
integral promotion could not be promoted
do not promote
condition s of these types
CWG 2629 C++98 condition could be a declaration of a floating-point variable prohibited

[edit] See also

Источник

Оператор switch (C)

Операторы switch и case помогают управлять сложными условными операциями и операциями ветвления. Оператор switch передает управление в оператор внутри своего тела.

Синтаксис

selection-statement :
switch ( expression ) statement

labeled-statement :
case constant-expression : statement
default : statement

Комментарии

Оператор switch передает управление одному из labeled-statement в своем теле в зависимости от значения expression .

Значения expression и значение каждого constant-expression должны иметь целочисленный тип. Выражение constant-expression должно иметь однозначное константное целочисленное значение во время компиляции.

Управление передается оператору case , значение constant-expression которого соответствует значению выражения expression . Оператор switch может содержать неограниченное число экземпляров case . Однако значения ни одной из пар выражений constant-expression в одном операторе switch не должны совпадать. Выполнение тела оператора switch начинается с первого соответствующего оператора labeled-statement или после него. Выполнение продолжается до конца тела оператора или пока оператор break не передаст управление за пределы тела.

Оператор switch обычно используется следующим образом:

Оператор break можно использовать для завершения обработки определенного оператора с меткой в операторе switch . Он выполняет ветвление до конца оператора switch . Без оператора break выполнение программы продолжается до следующего оператора с меткой. Операторы будут выполняться до достижения оператора break или конца всего оператора. В некоторых случаях может требоваться именно такое поведение.

Оператор default выполняется, если ни одно из значений case constant-expression не равно значению expression . Если оператор default не указан и соответствующий вариант case не найден, ни один из операторов в теле оператора switch не выполняется. Допускается не более одного оператора default . Оператор default не обязательно должен находиться в конце. Он может располагаться в любом месте тела оператора switch . Метка case или default может располагаться только внутри оператора switch .

Выражения switch expression и case constant-expression должны быть целочисленного типа. Значение каждого case constant-expression в теле оператора должно быть уникальным.

Метки case и default тела оператора switch имеют значения только при начальной проверке, которая определяет, с какого места тела оператора начинается выполнение. Операторы switch могут быть вложенными. Любые статические переменные инициализируются до выполнения любых операторов switch .

Объявления могут располагаться в начале составного оператора, образующего тело оператора switch , но инициализации, включенные в объявления, не выполняются. Оператор switch передает управление непосредственно на выполняемый оператор в теле, минуя все строки, содержащие инициализации.

В следующих примерах кода показаны операторы switch :

В этом примере, если переменная c равна ‘A’ , выполняются все три оператора тела switch , так как оператор break перед следующим оператором case отсутствует. Управления передается первому оператору ( capital_a++; ) и продолжается по-порядку во всей остальной части тела. Если переменная c равна ‘a’ , увеличиваются значения переменных letter_a и total . Если переменная c не равна ‘A’ или ‘a’ , увеличивается только значение переменной total .

В этом примере оператор break указан после каждого оператора тела switch . Оператор break вызывает принудительный выход из тела оператора после выполнения одного оператора. Если переменная i равна –1, увеличивается только значение переменной n . Оператор break после оператора n++; передает управление за пределы тела оператора, минуя оставшиеся операторы. Аналогично, если переменная i равна 0, увеличивается только значение переменной z ; если переменная i равна 1, увеличивается только значение переменной p . Заключительный оператор break , строго говоря, не требуется, так как управление передается из тела в конце составного оператора. Он добавлен для единообразия.

Один оператор может содержать несколько меток case , как показано в следующем примере:

В этом примере, если constant-expression равно любой букве между ‘a’ и ‘f’ , convert_hex вызывается функция .

Специально для систем Майкрософт

Microsoft C не ограничивает количество значений case в операторе switch . Это число ограничивается только объемом доступной памяти. ANSI C требует, чтобы в операторе switch можно было использовать не менее 257 меток case .

В Microsoft C расширения Майкрософт по умолчанию (default) включены. Используйте параметр компилятора /Za для отключения этих расширений.

Источник

Оцените статью