Espaços nominais
Variantes
Acções

switch statement

Da cppreference.com
< cpp‎ | language

 
 
Linguagem C++
Tópicos gerais
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controle de fluxo
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Declarações execução condicional
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
switch statement
Instruções de iteração
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ir declarações
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funções
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaração da função
lambda declaração da função
modelo de função
linha especificador
especificações de exceção (obsoleta)
noexcept especificador (C++11)
Exceções
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Especificadores
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv especificadores
armazenamento duração especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialização
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literais
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressões
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representações alternativas
Utilitários
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
tipo de alias declaração (C++11)
atributos (C++11)
Conversões
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversões implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Elenco C-estilo e funcional
Alocação de memória
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classe propriedades específicas de função
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funções membro especiais
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelos
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
modelo de classe
modelo de função
especialização de modelo
pacotes de parâmetros (C++11)
Diversos
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Assembly embutido
 
Executa o código de acordo com o valor de um argumento integral
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.
Utilizado onde um ou vários de muitos ramos de código precisa ser executado de acordo com um valor integral.
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.

Índice

[editar] Sintaxe

switch ( expression ) {
case constant_expression1 :
statement1 (opcional)
case constant_expression2 :
statement2 (opcional)
... ... ...
case constant_expressionn :
statementn (opcional)
default: default_statement (opcional)

}

[editar] Explicação

expression deve ser uma expressão, convertível para um valor inteiro.
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.
Todos os constant_expressions será expressões constantes, conversíveis para um valor inteiro, que é único nesta declaração switch
Original:
All constant_expressions shall be constant expressions, convertible to an integer value, which is unique within this switch statement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Se o expression avaliada como um valor, igual ao valor de uma das constant_expressioni definido, o statementi (se presente) e todas as afirmações seguintes (excepto default_statement, se presente) são executados. Se o valor do expression não corresponde a nenhuma das constant_expressions, o default_statement é executado se presente.
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.
pode ser usado. Nesse caso, a execução da instrução switch termina.
Original:
It is useful to note, that if the execution of subsequent statements is undesirable, the
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Palavras-chave

switch, case, default

[editar] Exemplo

O código a seguir mostra vários casos de uso da chave declaração
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.

#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
    }
}

Saída:

2345
d