Computer Science I / Conditionals and Logical Branching
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.

Hints
  1. An elif chain tests its conditions in order from the top.
  2. The first branch whose condition is true runs, and the rest are skipped.
Show the answer
  1. Test score >= 90: false, so skip the A branch
  2. Test score >= 80: true
  3. Run the B branch
  4. 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.

Read the lesson: Conditionals and Logical Branching →

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