1. Prof.OrlandoJr.(orlandodasilvajr@gmail.com)
Leitura e escrita de dados
int x; // Declaração da variável
cout << “Digite o valor de x:”; // Entrada de dados
cin >> x; // Saída de dados
cout << “O valor de x é ” << x; // Saída de dados
Principais tipos de dados em C++
Tipo Descrição Tamanho Faixa de valores
int Número inteiro
4 bytes
De -2147483648 a
2147483647
float Número de ponto
flutuante
4 bytes
Até 7 casas
decimais
double Número de ponto
flutuante de dupla
precisão
8 bytes Até 15 casas
decimais
bool Valor booleano 1 bytes true / false
char Caractere 1 byte -
string Cadeia de caracteres n bytes -
Operadores aritméticos
int x, y, z;
x = 20;
y = 5;
z = 6;
cout << “x + y = ” << x + y << endl; // x + y = 25
cout << “x – y = ” << x – y << endl; // x – y = 15
cout << “x * y = ” << x * y << endl; // x * y = 100
cout << “x / y = ” << x / y << endl; // x / y = 4
cout << “x % y = ” << x % y << endl; // x % y = 0
cout << “x / z = ” << x / z << endl; // x / z = 3
cout << “x % z = ” << x % z << endl; // x % z = 2
Estruturas de controle de seleção
int x = 10;
if (x >= 10){
cout << “É maior ou igual a 10”;
} else {
cout << “É menor que 10”;
}
int x = 60;
if (x > 50 && x < 70){
cout << “Entre 50 e 70”;
}
int x = 100;
if (x == 100 || x > 200){
cout << “É igual a 100 ou maior que 200”;
}
int x = 3;
switch(x){
case 1:
cout << “O valor de X é 1”;
break;
case 2:
cout << “O valor de X é 2”;
break;
case 3:
cout << “O valor de X é 3”;
break;
default:
cout << “O valor é maior que 3”;
}
2. Prof.OrlandoJr.(orlandodasilvajr@gmail.com)
Estruturas de controle de repetição
Estrutura – Código de exemplo Saída
int x = 0;
while (x < 5){
cout << x << endl;
x = x + 1;
}
0
1
2
3
4
int y = 0;
do {
cout << y << endl;
y++;
} while (y < 5);
0
1
2
3
4
5
for (int i = 0; i < 5; i++){
cout << i << endl;
}
0
1
2
3
4
5
cout << "Numeros pares" << endl;
for (int i = 0; i <= 10; i++){
if (i % 2 == 0){
cout << i << endl;
}
}
0
2
4
6
8
10
int x = 5;
while (x < 5){
cout << x << endl;
}
int z = 5;
do {
cout << z << endl;
} while (z < 5);
5
int x = 0, y = 10;
while (x < 5 && y > 5){
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << endl;
x++;
y--;
}
x = 0
y = 10
x = 1
y = 9
x = 2
y = 8
x = 3
y = 7
x = 4
y = 6