Decisão e Repetição
Estruturas de Decisão
const number1 = 10;
const number2 = 10;
const operator = '+'; // (+, -)
// decision
console.log(result); //=> 20
const number1 = 10;
const number2 = 10;
const operator = '+'; // (+, -)
// decision
console.log(result); //=> 20
if
if (operator === '+') {
result = number1 + number2;
} else if (operator === '-') {
result = number1 - number2;
} else {
result = 'Invalid operator';
}
if (operator === '+') {
result = number1 + number2;
} else if (operator === '-') {
result = number1 - number2;
} else {
result = 'Invalid operator';
}
switch
switch (operator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
default:
result = 'Invalid operator';
}
switch (operator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
default:
result = 'Invalid operator';
}
Estrutura de Repetição
while
let flag = 1;
while (flag <= 10) {
console.log(flag);
flag += 1;
}
let flag = 1;
while (flag <= 10) {
console.log(flag);
flag += 1;
}
do…while
let flag = 1;
do {
console.log(flag);
flag += 1;
} while (flag < 10);
let flag = 1;
do {
console.log(flag);
flag += 1;
} while (flag < 10);
for
for (let flag = 1; flag <= 10; flag += 1) {
console.log(flag);
}
for (let flag = 1; flag <= 10; flag += 1) {
console.log(flag);
}
Editar esta página