Practice question · Put in order
A grading program runs: if score >= 90 give A; elif score >= 80 give B; elif score >= 70 give C; else give F. For a score of 85, order what the program does.
- Skip every remaining branch, including the else
- Test score >= 80: true
- Test score >= 90: false, so skip the A branch
- Run the B branch
Hints
- An elif chain tests its conditions in order from the top.
- The first branch whose condition is true runs, and the rest are skipped.
Show the answer
- Test score >= 90: false, so skip the A branch
- Test score >= 80: true
- Run the B branch
- Skip every remaining branch, including the else
Why
Conditions are checked top to bottom; the first true one (score >= 80) runs and every later branch, else included, is skipped. This is why order matters in an elif chain, putting a looser condition first would catch cases meant for a later branch.
Practise Conditionals and Logical Branching
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Conditionals and Logical Branching
- With short-circuit evaluation, in the expression A or B, if A is true then B is still always evaluated.
- Sort each operator by its role.
- Python makes if x = 5 a syntax error while C compiles it happily. What is C's version actually doing?
- if x = 5 is a common bug in languages where it is legal, while if x == 5 is what was meant. Why is the…