Curriculum

Curriculum

Write Pseudocode for a Loop and Running Count

Scenario

Write pseudocode to count how many rooms in seat_counts have seats > 0.

  1. 1…
  2. 2…
  3. 3…
  4. 4…
Pseudocode
FUNCTION count_open_rooms(seat_counts):
  SET open_count = 0
  FOR EACH seats IN seat_counts:
    IF seats > 0 THEN open_count = open_count + 1
  RETURN open_count

Try it yourself

Order the four pseudocode blocks from setup to final return.

Reveal the explanation
  1. SET open_count = 0

    Initializes the counter once before visiting any item.

  2. FOR EACH seats IN seat_counts:

    Visits every room count cleanly—including 0 passes on an empty list.

  3. IF seats > 0 THEN open_count = open_count + 1

    Updates the counter only for open rooms.

  4. RETURN open_count (after the loop)

    Returns the complete count once every room has been inspected.

Indentation matters in pseudocode: putting RETURN inside the loop stops on the very first room instead of counting the full list.

Review first

Your turn

Write 4-line pseudocode for count_passing(scores) counting scores >= 60.

Common mistake Indenting RETURN inside the FOR EACH block so the function returns after checking only scores[0].

Check your work

SET passed = 0; FOR EACH s IN scores: IF s >= 60 THEN passed = passed + 1; RETURN passed (unindented).

Quick check

What happens if RETURN open_count is indented inside the FOR EACH loop?
Show the answer and reasoning

Correct answer The function exits on the very first item and skips the rest of the list

A return inside the loop terminates execution during pass 1.