Baixe o app para aproveitar ainda mais
Prévia do material em texto
Introdução a Programação Fabiano Gonçalves dos Santos Aula 6 * Estruturas condicionais (continuação) Operador ternário * if (x == 10) // Se x for igual a 10 y = 20; // então faço y receber 20 else y = 30; // senão faço y receber 30 y = (x == 10) ? 20 : 30; * * 1 #include <iostream> 2 using namespace std; 3 4 int main(void){ 5 int idade; 6 char sexo; 7 8 cout<<"Qual a sua idade?"<<endl; 9 cin>>idade; 10 cout<<"Digite (m)para Masculino, (f)para Feminino"<<endl; 11 cin>>sexo; 12 13 if (idade>=18){ 14 cout<<"Voce pode dirigir"<<endl; 15 16 if (sexo=='m'){ 17 cout<<"Voce precisa ir no Servico Militar"<<endl; 18 } 19 } 20 else { 21 if (idade>16){ 22 cout<<"Voce ja pode votar"<<endl; 23 } 24 else { 25 cout<<"Voce nao pode votar, nem dirigir"<<endl; 26 } 27 } 28 return 0; 29 } Aninhando IFs * Switch * switch (variável){ case constante1: Instruções; break; case constante2: Instruções; break; default Instruções; } #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]){ int valor = 4; switch(valor){ case 0: cout << "Valor e igual a 0" << endl; break; case 1: cout << "Valor e igual a 1" << endl; break; case 2: cout << "Valor é igual a 2" << endl; break; default: cout << "Nenhuma das anteriores" << endl; } system("PAUSE"); return EXIT_SUCCESS; } * Operadores lógicos * ((2 > 1) || (3 < 7)) : resultado VERDADEIRO ((3 < 2) && (2 == 2)) : resultado FALSO ((5 !=0) || (1 < 2)) : resultado VERDADEIRO !(2 > 1): resultado VERDADEIRO !(1 < 0): resultado FALSO Introdução a Programação Fabiano Gonçalves dos Santos Atividade 6 * It’s time! Vamos praticar! Usando o Dev C++ * © Alphaspirit | Dreamstime.com
Compartilhar