Selection · Seleção
Programs make choices
- Selection means the program picks what to do based on a condition.
- A condition is a question that is
TrueorFalse. - This is a key idea in AP CSP: code that decides.
Programas fazem escolhas
- Seleção significa que o programa escolhe o que fazer baseado numa condição.
- Uma condição é uma pergunta que é
TrueouFalse. - Esta é uma ideia-chave no AP CSP: código que decide.
if / elif / else
ifruns its block only when the condition isTrue.elif("else if") checks another condition if the ones above wereFalse.elseruns when none of the conditions wereTrue.
if / elif / else
ifexecuta seu bloco apenas quando a condição éTrue.elif("else if") verifica outra condição se as anteriores fossemFalse.elseexecuta quando nenhuma das condições eraTrue.
hour = 14
if hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
else:
print("Evening")
Comparisons make conditions
==equal,!=not equal,<><=>=.==tests if two values are equal; a single=stores a value.- The result of a comparison is
TrueorFalse.
Comparações criam condições
==igual,!=diferente,<><=>=.==testa se dois valores são iguais; um único=armazena um valor.- O resultado de uma comparação é
TrueouFalse.
score = 72
print(score >= 50)
print(score == 100)
Boolean logic: and / or / not
andisTrueonly when both sides areTrue.orisTruewhen at least one side isTrue.notflips it:not TrueisFalse.
Lógica booleana: and / or / not
andéTrueapenas quando ambos os lados sãoTrue.oréTruequando pelo menos um dos lados éTrue.notinverte:not TrueéFalse.
age = 16
if age >= 13 and age <= 19:
print("Teenager")
else:
print("Not a teenager")
In AP CSP pseudocode
- Python
if/elsematches CB'sIF(condition){ } ELSE { }. - CB uses words for logic:
AND,OR,NOT. DISPLAY= print,←==.
Em pseudocódigo AP CSP
- Python
if/elsecorresponde aoIF(condition){ } ELSE { }do CB. - CB usa palavras para lógica:
AND,OR,NOT. DISPLAY= print,←==.
IF (score >= 50)
{
DISPLAY "Pass"
}
ELSE
{
DISPLAY "Fail"
}
IF ((age >= 13) AND (age <= 19))
{
DISPLAY "Teenager"
}
Common mistakes
- Compare with
==, not=. - Indent the block that runs when the condition is true.
Erros comuns
- Compare com
==, não=. - Indente o bloco que executa quando a condição é verdadeira.
Now you try
- Read each task to see what to print or check.
- Match the output text exactly.
Agora você tenta
- Leia cada tarefa para ver o que imprimir ou verificar.
- Corresponda ao texto de saída exatamente.
Selection picks a branch · Seleção escolhe um ramo
Conditions are tested in order; the first true one runs. · Condições são testadas na ordem; a primeira verdadeira executa.
Read a score with int(input()). Print A if it is 80 or more, B if it is 60 or more, otherwise C. Use if / elif / else. For input 75, print B. · Leia uma pontuação com int(input()). Imprima A se for 80 ou mais, B se for 60 ou mais, caso contrário C. Use if / elif / else. Para entrada 75, imprima B.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
temp is 22. Set comfortable to · até True when temp is from 18 to · até 25 (inclusive), else False. Use one comparison with and. · temp é 22. Defina comfortable como True quando temp estiver de 18 a 25 (inclusivo), senão False. Use uma comparação com and.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
day is "Sun" and · e is_holiday is False. Print Day off if it is a weekend ("Sat" or · ou "Sun") or · ou a holiday; otherwise print Work day. Use or. · day é "Sun" e is_holiday é False. Imprima Day off se for fim de semana ("Sat" ou "Sun") ou feriado; caso contrário imprima Work day. Use or.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.