Selection · Seleksi
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.
Program membuat pilihan
- Selection berarti program memilih apa yang harus dilakukan berdasarkan kondisi.
- Kondisi adalah pertanyaan yang
TrueatauFalse. - Ini adalah ide kunci dalam AP CSP: kode yang memutuskan.
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
ifmenjalankan bloknya hanya ketika kondisinyaTrue.elif("else if") memeriksa kondisi lain jika kondisi di atasnyaFalse.elsedijalankan ketika tidak ada kondisi yangTrue.
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.
Perbandingan membuat kondisi
==sama,!=tidak sama,<><=>=.==menguji apakah dua nilai sama; satu=menyimpan nilai.- Hasil perbandingan adalah
TrueatauFalse.
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.
Logika Boolean: and / or / not
andadalahTruehanya ketika kedua sisi adalahTrue.oradalahTrueketika setidaknya satu sisi adalahTrue.notmembaliknya:not TruemenjadiFalse.
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,←==.
Dalam pseudocode AP CSP
- Python
if/elsecocok dengan CB'sIF(condition){ } ELSE { }. - CB menggunakan kata untuk logika:
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.
Kesalahan umum
- Bandingkan dengan
==, bukan=. - Indentasi blok yang dijalankan ketika kondisi benar.
Now you try
- Read each task to see what to print or check.
- Match the output text exactly.
Sekarang Anda coba
- Baca setiap tugas untuk melihat apa yang harus dicetak atau dicek.
- Cocokkan teks output tepat.
Selection picks a branch · Seleksi memilih sebuah cabang
Conditions are tested in order; the first true one runs. · Kondisi diuji secara berurutan; yang pertama benar akan dieksekusi.
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. · Baca skor dengan int(input()). Cetak A jika nilainya 80 atau lebih, B jika 60 atau lebih, sebaliknya cetak C. Gunakan if / elif / else. Untuk input 75, cetak B.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
temp is 22. Set comfortable to True when temp is from 18 to 25 (inclusive), else False. Use one comparison with and. · temp adalah 22. Tetapkan comfortable ke True ketika temp berada antara 18 sampai 25 (termasuk), selain itu False. Gunakan satu perbandingan dengan and.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
day is "Sun" and is_holiday is False. Print Day off if it is a weekend ("Sat" or "Sun") or a holiday; otherwise print Work day. Use or. · day adalah "Sun" dan is_holiday adalah False. Cetak Day off jika itu akhir pekan ("Sat" atau "Sun") atau hari libur; kalau tidak cetak Work day. Gunakan or.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.