Lesson 74: Order of the tests
Halaman ini memerlukan browser terbaru (dengan dukungan SharedArrayBuffer). Silakan perbarui Chrome, Edge, Firefox, atau Safari ke versi terbaru.
English
The first true test wins
- In an
if ... else if ... elsechain, the board checks the tests in order and runs only the first one that is true. - So put the strictest test first. 91 is ≥ 40, ≥ 60 and ≥ 80: if
score >= 40comes first, 91 gets a C.
| Score | Wrong order (≥ 40 first) | Right order (≥ 80 first) |
|---|---|---|
| 21 | D | D |
| 46 | C | C |
| 63 | C | B |
| 91 | C | A |
English
CASE in C++
- A CASE statement picks a branch from the value of one variable. In C++ it is
switch. default:is like OTHERWISE.break;ends each case.
English
In the exam
- 0478/21/M/J/25 Q10: (a) trace the grades flowchart with 21, 46, 63, 91, 12. Mark scheme: D, C, C, C, D: every score of 40 or more gets a C. (b) Correct it. Mark scheme: the first decision becomes Score >= 80, the last decision becomes Score >= 40, and the grades in their boxes change to match (A first, C last). Your first program is the corrected flowchart.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
This is the exam flowchart for grades as code: A for 80 or more, B for 60 or more, C for 40 or more, otherwise D. Type a score. It gives C for 63 and for 91: the first test, score >= 40, is already true for them, so the other tests never run. Change the order of the tests so every score gets the right grade.
Klik Jalankan untuk melihat output di sini.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
After the grade, print a comment with a switch statement (CASE OF in pseudocode): A Excellent, B Good, C Pass, anything else Try again.
Klik Jalankan untuk melihat output di sini.