switch statement
Материал из cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Выполняет код по значению аргумента
Original:
Executes code according to value of an integral argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Используется там, где одна или несколько из многих отраслей кода должны быть выполнены в соответствии с интегральное значение.
Original:
Used where one or several out of many branches of code need to be executed according to an integral value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Содержание |
[править] Синтаксис
switch ( expression ) {
|
|||||||||
[править] Объяснение
expression должно быть выражение, конвертируемые в целое число.
Original:
expression shall be an expression, convertible to an integer value.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Все constant_expressions должен быть постоянным выражения, конвертируемые в целое число, которое является уникальным в пределах этого
switch заявленииOriginal:
All constant_expressions shall be constant expressions, convertible to an integer value, which is unique within this
switch statementThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Если expression вычисляет значение, равное значению одного из определено constant_expressioni, statementi (если имеется) и все последующие заявления (за исключением default_statement, если он присутствует) выполняются. Если значение expression не соответствует ни одному из constant_expressions, default_statement выполняется, если настоящим.
Original:
If the expression evaluates to a value, equal to the value of one of the defined constant_expressioni, the statementi (if present) and all subsequent statements (except default_statement, if present) are executed. If the value of the expression does not match any of the constant_expressions, the default_statement is executed if present.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Полезно отметить, что если выполнение последующих заявлениях нежелательно, <div class="t-tr-text"> сломать заявлении
могут быть использованы. В этом случае выполнение Original:
break statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
switch оператор завершает. Original:
It is useful to note, that if the execution of subsequent statements is undesirable, the
сломать заявлении</div> can be used. In that case the execution of the
Original:
break statement
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
switch statement terminates. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Ключевые слова
[править] Пример
В следующем коде показано несколько случаев использования' переключатель заявлении
Original:
The following code shows several usage cases of the switch statement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream> int main() { int i = 2; switch (i) { case 1: std::cout << "1"; case 2: std::cout << "2"; //execution starts at this case label case 3: std::cout << "3"; case 4: 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_statement is executed } std::cout << '\n'; switch (i) { case 4: std::cout << "a"; //nothing is executed } }
Вывод:
2345 d