Curriculum

Curriculum

Put an Invalid-Input Guard First in Pseudocode

Scenario

Design pseudocode for can_book(free_seats) with explicit negative-number rejection.

  1. 1…
  2. 2…
  3. 3…
Pseudocode
FUNCTION can_book(free_seats):
  IF free_seats IS NOT INTEGER OR free_seats < 0 THEN
    RETURN ERROR "INVALID_FREE_SEATS"
  RETURN free_seats > 0

Try it yourself

Order the guard clause ahead of the core seat check.

Reveal the explanation
  1. IF free_seats < 0 THEN RETURN ERROR "INVALID_FREE_SEATS"

    Rejects impossible input before any booking decision runs.

  2. IF free_seats > 0 THEN RETURN true

    Core positive booking rule runs only on verified non-negative counts.

  3. RETURN false

    Clean fallback for exact full room (free_seats == 0).

Early guard clauses prevent deep nesting and ensure invalid inputs never reach core business logic or produce misleading false results.

Review first

Your turn

Add an early guard clause to did_pass(score) rejecting score < 0 or score > 100 with "INVALID_SCORE".

Common mistake Checking score >= 60 first so score = 150 returns Pass before the guard runs.

Check your work

IF score < 0 OR score > 100 THEN RETURN ERROR "INVALID_SCORE"; RETURN score >= 60.

Quick check

Why must "IF score > 100 THEN ERROR" come BEFORE "IF score >= 60 THEN Pass"?
Show the answer and reasoning

Correct answer Otherwise score = 120 matches >= 60 first and wrongly passes

Placing the guard first stops out-of-range inputs before they match valid branches.