Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anterior Revisão anterior Próxima revisão | Revisão anterior | ||
c:controle_de_fluxo [2023/08/02 16:38] – maziero | c:controle_de_fluxo [2024/09/05 16:55] (atual) – maziero | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ====== Controle de fluxo em C ====== | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ===== Decisões ===== | ||
+ | |||
+ | === comando if === | ||
+ | |||
+ | <code c> | ||
+ | if (expression) | ||
+ | command ; | ||
+ | </ | ||
+ | |||
+ | ou | ||
+ | |||
+ | <code c> | ||
+ | if (expression) | ||
+ | { | ||
+ | command ; | ||
+ | command ; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <note important> | ||
+ | O comando " | ||
+ | </ | ||
+ | |||
+ | Ifs aninhados: | ||
+ | |||
+ | <code c> | ||
+ | if (expression1) | ||
+ | if (expression2) | ||
+ | { | ||
+ | command ; | ||
+ | command ; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Comando if-else === | ||
+ | |||
+ | <code c> | ||
+ | if (expression) | ||
+ | command1 ; // <--- there is a semicolon here! | ||
+ | else | ||
+ | command2 ; | ||
+ | </ | ||
+ | |||
+ | ou | ||
+ | |||
+ | <code c> | ||
+ | if (expression) | ||
+ | { | ||
+ | command1a ; | ||
+ | command1b ; | ||
+ | command1c ; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | command2a ; | ||
+ | command2b ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If-else aninhados: | ||
+ | |||
+ | <code c> | ||
+ | if (expression1) | ||
+ | if (expression2) | ||
+ | command1 ; | ||
+ | else | ||
+ | command2 ; | ||
+ | else | ||
+ | command3 ; | ||
+ | </ | ||
+ | |||
+ | <note important> | ||
+ | A cláusula '' | ||
+ | </ | ||
+ | |||
+ | === Comando switch === | ||
+ | |||
+ | <code c> | ||
+ | switch (expression) | ||
+ | { | ||
+ | case constant1: | ||
+ | command1 ; | ||
+ | ... | ||
+ | break ; | ||
+ | case constant2: | ||
+ | command2 ; | ||
+ | ... | ||
+ | break ; | ||
+ | case constant3: | ||
+ | case constant4: | ||
+ | command34a ; | ||
+ | ... | ||
+ | break ; | ||
+ | ... | ||
+ | default: | ||
+ | commandNa; | ||
+ | ... | ||
+ | break ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Exemplo (contagem de carros): | ||
+ | |||
+ | <code c> | ||
+ | ch = getchar(); | ||
+ | |||
+ | switch(ch) | ||
+ | { | ||
+ | case ' | ||
+ | case ' | ||
+ | corsa++ ; | ||
+ | break ; | ||
+ | case ' | ||
+ | prisma++ ; | ||
+ | break ; | ||
+ | case ' | ||
+ | palio++ ; | ||
+ | break ; | ||
+ | default: | ||
+ | outros++ ; | ||
+ | break ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Comando condicional ternário === | ||
+ | |||
+ | A expressão | ||
+ | |||
+ | <code c> | ||
+ | expression1 ? expression2 : expression3 ; | ||
+ | </ | ||
+ | |||
+ | é equivalente a | ||
+ | |||
+ | <code c> | ||
+ | if (expression1) | ||
+ | expression2 ; | ||
+ | else | ||
+ | expression3 ; | ||
+ | </ | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | x = x < 5 ? x + 1 : 1 ; | ||
+ | </ | ||
+ | |||
+ | equivale a: | ||
+ | |||
+ | <code c> | ||
+ | if (x < 5) | ||
+ | x = x + 1 ; | ||
+ | else | ||
+ | x = 1 ; | ||
+ | </ | ||
+ | |||
+ | ===== Laços ===== | ||
+ | |||
+ | === Comando while === | ||
+ | |||
+ | <code c> | ||
+ | while (expression) | ||
+ | command ; | ||
+ | </ | ||
+ | |||
+ | ou | ||
+ | |||
+ | <code c> | ||
+ | while (expression) | ||
+ | { | ||
+ | command1 ; | ||
+ | command2 ; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | i = 0 ; | ||
+ | while (i < 100) | ||
+ | { | ||
+ | printf ("i vale %d\n", i) ; | ||
+ | i++ ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Comando do-while === | ||
+ | |||
+ | <code c> | ||
+ | do | ||
+ | command ; | ||
+ | while (expression) ; | ||
+ | </ | ||
+ | |||
+ | ou | ||
+ | |||
+ | <code c> | ||
+ | do | ||
+ | { | ||
+ | command1 ; | ||
+ | command2 ; | ||
+ | ... | ||
+ | } | ||
+ | while (expression) ; | ||
+ | </ | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | i = 100 ; | ||
+ | do | ||
+ | { | ||
+ | printf ("i vale %d\n", i) ; | ||
+ | i-- ; | ||
+ | } | ||
+ | while i ; | ||
+ | </ | ||
+ | |||
+ | === Comando for === | ||
+ | |||
+ | <code c> | ||
+ | for (initializations; | ||
+ | command ; | ||
+ | </ | ||
+ | |||
+ | ou | ||
+ | |||
+ | <code c> | ||
+ | for (initializations; | ||
+ | { | ||
+ | command1 ; | ||
+ | command2 ; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Exemplo: um laço de repetição com a variável de controle indo de 0 a 99: | ||
+ | |||
+ | <code c> | ||
+ | for (i = 0; i < 100; i++) | ||
+ | printf ("i vale %d", i) ; | ||
+ | </ | ||
+ | |||
+ | Deve-se observar que o comando '' | ||
+ | |||
+ | <code c> | ||
+ | initializations; | ||
+ | while (conditions) | ||
+ | { | ||
+ | command1 ; | ||
+ | command2 ; | ||
+ | ... | ||
+ | updates ; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Então o exemplo acima pode ser reescrito como: | ||
+ | |||
+ | <code c> | ||
+ | i = 0 ; // inicialização | ||
+ | while (i < 100) // condição | ||
+ | { | ||
+ | printf ("i vale %d", i) ; | ||
+ | i++ ; // update | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Uma forma peculiar uso do '' | ||
+ | |||
+ | <code c> | ||
+ | for (;;) | ||
+ | { | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Desvios ===== | ||
+ | |||
+ | === Comando break === | ||
+ | |||
+ | O comando '' | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | for (;;) | ||
+ | { | ||
+ | printf (" | ||
+ | resp = getchar() ; | ||
+ | if(resp == ' | ||
+ | break; | ||
+ | printf (" | ||
+ | } | ||
+ | // o break salta para cá | ||
+ | </ | ||
+ | |||
+ | === Comando continue === | ||
+ | |||
+ | O comando '' | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | int i; | ||
+ | |||
+ | for (i = -10; i < 10; i++) | ||
+ | { | ||
+ | if (i == 0) | ||
+ | continue; // pula para a próxima iteração do laço | ||
+ | | ||
+ | printf(" | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Comando goto === | ||
+ | |||
+ | O comando '' | ||
+ | |||
+ | <code c> | ||
+ | goto PARTE2; | ||
+ | |||
+ | // ... (qualquer código) | ||
+ | |||
+ | PARTE2: | ||
+ | i = 0 ; | ||
+ | // ... | ||
+ | </ | ||
+ | |||
+ | <note warning> | ||
+ | O comando '' | ||
+ | </ | ||
+ | |||
+ | === Comando return === | ||
+ | |||
+ | O comando '' | ||
+ | |||
+ | Exemplo: | ||
+ | |||
+ | <code c> | ||
+ | // compara dois inteiros " | ||
+ | // +1 se a > b | ||
+ | // -1 se a < b | ||
+ | // 0 se a = b | ||
+ | |||
+ | int compara (int a, b) | ||
+ | { | ||
+ | if (a > b) return | ||
+ | if (a < b) return -1 ; | ||
+ | return | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Comando exit === | ||
+ | |||
+ | A função '' | ||
+ | |||
+ | <code c> | ||
+ | exit (0) ; | ||
+ | </ | ||
+ | |||
+ | |||