Selection — IF and CASE · Pemilihan — IF dan 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.
Membuat keputusan
- Seleksi berarti memilih apa yang harus dilakukan, berdasarkan tes.
- An
ifmenjalankan bloknya hanya ketika tes tersebut benar. - Tes memberikan Boolean:
TrueatauFalse.
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: jalur lain
elseberjalan ketika tesifsalah.- Tepat salah satu dari dua blok yang berjalan.
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: banyak jalur
- Gunakan
elifuntuk menguji lebih banyak kondisi secara berurutan. - Python memeriksa setiap tes dari atas ke bawah dan berhenti pada yang pertama benar.
mark = 72
if mark >= 70:
print("A")
elif mark >= 50:
print("B")
else:
print("C")
if / elif / else — which runs? · if / elif / else — mana yang berjalan?
Tests run top-down; the first true one runs, the rest are skipped. · Uji berjalan dari atas ke bawah; yang pertama benar akan berjalan, sisanya dilewati.
Comparison operators
==means equal to (the exam writes=).!=means not equal to (the exam writes<>).- Also use
<,>,<=,>=.
Operator perbandingan
==berarti sama dengan (ujian menulis=).!=berarti tidak sama dengan (ujian menulis<>).- Juga gunakan
<,>,<=,>=.
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.
Dalam ujian: IF dan CASE
- Pseudocode menggunakan
IF … THEN … ELSE … ENDIF, atauCASEuntuk banyak pilihan tetap.
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.
Kesalahan umum
- Bandingkan dengan
==, bukan=. - Cakup setiap kasus, termasuk
else. - indentasi blok yang milik setiap cabang.
Now you try
- Read the input, then choose what to print with
if/elif/else. - Press Check answer to test your code.
Sekarang Anda coba
- Baca input, lalu pilih apa yang akan dicetak dengan
if/elif/else. - Tekan Periksa jawaban untuk menguji kode Anda.
Read a whole number, then print even if it divides by 2 with no remainder, otherwise print odd. For input 4, print even. · Baca bilangan bulat, lalu cetak even jika habis dibagi 2 tanpa sisa, jika tidak cetak odd. Untuk input 4, cetak even.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Baca satu markah (0–100). Cetak A untuk 70 atau lebih, B untuk 50–69, dan C untuk apa jua yang kurang. Untuk input 65, cetak B.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Two numbers a and b are set for you. Use an if to store the larger one in biggest. · Dua bilangan a dan b diatur untuk Anda. Gunakan if untuk menyimpan yang lebih besar di biggest.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.