Baixe o app para aproveitar ainda mais
Prévia do material em texto
Introdução a Programação Fabiano Gonçalves dos Santos Aula 7 * Objetivos Estrutura for Operadores de incremento e decremento * * Loopings * © Paul Brennan | Dreamstime.com Também chamados de Laços Repetições © Oleksandr Melnyk | Dreamstime.com * Loopings * © Oleksandr Melnyk | Dreamstime.com * O for * 1 #include <iostream> 2 using namespace std; 3 4 int main (void){ 5 for (int i=1; i<=10; i++) 6 cout<<i<<", "; 7 8 return 0; 9 } * O for Sintaxe for (<inicialização>; <condição>; <incremento>) <comandos> * * Operadores unários * * * 1 #include <iostream> 2 using namespace std; 3 4 int main (void){ 5 int i; 6 7 i=5; 8 cout<<i<<endl; 9 cout<<i++<<endl; 10 cout<<i<<endl; 11 12 cout<<"------------"<<endl; 13 14 i=5; 15 cout<<i<<endl; 16 cout<<++i<<endl; 17 cout<<i<<endl; 18 return 0; 19 } * Exemplos Supondo x=4, y=6, z=5, w=7, t=13: x+=7 x = x+7 x=11 y-=4 y = y-4 y=2 z*=5 z = z*5 z=25 w/=7 w = w/7 w=1 t%=4 t = t%4 t=1 * * Mais exemplos * 1 #include <iostream> 2 using namespace std; 3 4 int main (void){ 5 int total = 0; 6 7 for (int n=2; n<=20; n+=2){ 8 total += n; 9 } 10 cout<<"Soma = "<<total; 11 return 0; 12 } Introdução a Programação Fabiano Gonçalves dos Santos Atividade 7 * * It’s time! Vamos praticar! Usando o Dev C++ * © Alphaspirit | Dreamstime.com
Compartilhar