Nested decisions · Keputusan bersarang
One mark, three outcomes
- A mark of 80 or more is a distinction, 50 or more is a pass, and anything lower is a fail.
- One
IFhas two paths. The second decision goes inside theELSE, as its ownIF. - Each
IFhas its ownENDIF. Indent the inner one so you can see whichELSEit belongs to.
Satu nilai, tiga hasil
- Nilai 80 atau lebih adalah pembedaan, 50 atau lebih adalah lulus, dan apa pun yang lebih rendah adalah gagal.
- Satu
IFmemiliki dua jalur. Keputusan kedua berada di dalamELSE, sebagaiIF-nya sendiri. - Setiap
IFmemilikiENDIF-nya sendiri. Berikan indentasi pada yang dalam agar Anda dapat melihat keELSEmana itu milik.
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIF
Count the marks that count
- A validation tally walks the list and adds 1 only when the value is in range.
- The range test is the
ANDfrom the previous lesson, inside the loop.
Hitung nilai yang dihitung
- Penghitung validasi melintasi daftar dan menambahkan 1 hanya jika nilainya berada dalam rentang.
- Uji rentang adalah
ANDdari pelajaran sebelumnya, di dalam loop.
DECLARE Mark : ARRAY[1:4] OF INTEGER
DECLARE I, Count : INTEGER
Mark[1] ← 80
Mark[2] ← -1
Mark[3] ← 100
Mark[4] ← 40
Count ← 0
FOR I ← 1 TO 4
IF Mark[I] >= 0 AND Mark[I] <= 100
THEN
Count ← Count + 1
ENDIF
NEXT I
OUTPUT Count // → 3
Watch out
- The inner
IFstarts on the line afterELSE. It is not written on theELSEline. - Close the inner
ENDIFbefore the outer one. The lastENDIFbelongs to the firstIF.
Hati-hati
IFdalam dimulai pada baris setelahELSE. Tidak ditulis pada barisELSE.- Tutup
ENDIFdalam sebelum yang luar.ENDIFterakhir milikIFpertama.
Common mistakes
- An
ENDIFin the wrong place closes the outer test and leaves the inner one open. - A mark of exactly 50 is a pass, and exactly 80 is a distinction. The tests use
>=.
Kesalahan umum
ENDIFdi tempat yang salah menutup uji luar dan meninggalkan yang dalam tetap terbuka.- Nilai tepat 50 adalah lulus, dan tepat 80 adalah pembedaan. Uji menggunakan
>=.
Now you try
- The first two tasks are nested decisions. The third counts with
AND. - Press Check answer to test it.
Sekarang Anda coba
- Dua tugas pertama adalah keputusan bersarang. Tugas ketiga dihitung dengan
AND. - Tekan Cek jawaban untuk mengujinya.
Read an INTEGER Mark. Output Distinction for 80 or more, otherwise Pass for 50 or more, otherwise Fail. Nest the second IF under ELSE. · Baca satu INTEGER Mark. Keluarkan Distinction untuk 80 atau lebih, sebaliknya Pass untuk 50 atau lebih, sebaliknya Fail. Letakkan IF kedua di bawah ELSE.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Same three outcomes as the previous task. This input should take the inner ELSE. · Tiga hasil yang sama seperti tugas sebelumnya. Input ini harus mengambil ELSE bagian dalam.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
The array Mark holds 80, -1, 100, 40. Count how many values are from 0 to 100 inclusive and output the count (it is 3). · Array Mark berisi 80, -1, 100, 40. Hitung berapa banyak nilai dari 0 hingga 100 (termasuk) dan output jumlah tersebut (yaitu 3).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.