Write Pseudocode for a Loop and Running Count
Scenario
Write pseudocode to count how many rooms in seat_counts have seats > 0.
- 1…
- 2…
- 3…
- 4…
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_countTry it yourself
Order the four pseudocode blocks from setup to final return.
Reveal the explanation
- SET open_count = 0
Initializes the counter once before visiting any item.
- FOR EACH seats IN seat_counts:
Visits every room count cleanly—including 0 passes on an empty list.
- IF seats > 0 THEN open_count = open_count + 1
Updates the counter only for open rooms.
- 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.
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
This browser could not save your progress. Changes last until you leave the page.