Selection — IF and CASE · Seleção — IF e CASE
Making a decision
- Selection means choosing what to do, based on a test.
- An
ifruns its block only when the test is true. - The test gives a Boolean:
TrueorFalse.
Tomando decisões
- Seleção significa escolher o que fazer, baseado em um teste.
- Um
ifexecuta seu bloco apenas quando o teste for verdadeiro. - O teste retorna um Booleano:
TrueouFalse.
age = 20
if age >= 18:
print("You may vote")
else: the other path
elseruns when theiftest is false.- Exactly one of the two blocks runs.
else: o outro caminho
elseexecuta quando o testeiffor falso.- Exatamente um dos dois blocos é executado.
mark = 45
if mark >= 50:
print("Pass")
else:
print("Fail")
elif: many paths
- Use
elifto test more conditions in order. - Python checks each test top to bottom and stops at the first true one.
elif: muitos caminhos
- Use
elifpara testar mais condições em sequência. - Python verifica cada teste de cima para baixo e para no primeiro verdadeiro.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — qual será executado?
Tests run top-down; the first · primeiro true one runs, the rest are skipped. · Os testes são executados de cima para baixo; o primeiro verdadeiro é executado, os demais são pulados.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
Operadores de comparação
==significa igual a (a prova escreve=).!=significa diferente de (a prova escreve<>).- Também use
<,>,<=,>=.
print(5 == 5) # True
print(5 != 3) # True
print(3 <= 2) # False
In the exam: IF and CASE
- Pseudocode uses
IF … THEN … ELSE … ENDIF, orCASEfor many fixed choices.
Na prova: IF e CASE
- Pseudocódigo usa
IF … THEN … ELSE … ENDIF, ouCASEpara muitas escolhas fixas.
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Common mistakes
- Compare with
==, not=. - Cover every case, including the
else. - Indent the block that belongs to each branch.
Erros comuns
- Compare com
==, não=. - Cubra todos os casos, incluindo o
else. - Indente o bloco pertencente a cada ramificação.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
Agora você tenta
- Leia a entrada, depois escolha o que imprimir com
if/elif/else. - Clique em Check answer para testar seu código.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · Leia um número inteiro, depois imprima even se ele for divisível por 2 sem resto, caso contrário imprima odd. Para entrada 4, imprima even.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Read a mark (0–100). Print A for 70 or more, B for 50–69, and C for anything less. For input 65, print B. · Leia uma nota (0–100). Imprima A para 70 ou mais, B para 50–69, e C para qualquer valor menor. Para entrada 65, imprima B.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Two numbers a and · e b are set for you. Use an if to store the larger · maior one in biggest. · Dois números a e b estão definidos para você. Use um if para armazenar o maior deles em biggest.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.