Lecture 06 — Traces and Loop Reasoning

Position in the course

This is lecture 6 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 04–05 — Python control flow and arithmetic

Assumed fluencies:

Preparation task: Trace cs-021 by hand once before class.

Detailed topic outline

Core (mandatory):

  1. the state table as a universal tracer
  2. loop invariants informally
  3. nested loops and grid thinking
  4. off-by-one taxonomy

Extension (optional enrichment):

  1. termination questions
  2. reading code you did not write

Case sequence

CaseTitleDifficulty
cs-021Trace the TallyBeginner
cs-022The Shrinking LoopFoundational
cs-023Nested and ConfusedIntermediate
cs-024The Off-By-One MuseumAdvanced

Display each case full-screen from the student page (cases cs-021–cs-024); instructor pages cs-021–cs-024 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-021): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-022): same rhythm
50–70Case 3 (cs-023): same rhythm, harder case
70–75Break
75–100Case 4 (cs-024): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Trace cs-021 with a full state table on the board, then delete a column and let the class feel what is lost.

Classroom activities

#ActivityMinutesFormat and prompt
1Think-Pair-Debug8Run activities/think-pair-debug.md on a one-bug variant of cs-021.
2State-table race6Same snippet, two teams, who completes a correct state table first — then compare columns.
3Off-by-one museum tour7Walk the four exhibits of cs-024; for each, the class states the input class that triggers it.

Board plan

Instructor explanation notes

Reading the room. Tracing is the course's most transferable skill. The state table is non-negotiable in week one of tracing: columns for every variable, one row per iteration, no skipping.

Running the reveal. Advanced students should be pushed from 'what does it print' to 'why must it terminate' — the invariant vocabulary starts here informally.

Lecture focus. One sentence to repeat verbatim: The state table is the honest record of a loop's life.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. Produce a full state table for a provided shrinking-loop snippet (cs-022 family).
  2. Trace the nested grid loop of cs-023 for n = 3 and count the cells visited.
  3. Write the one-sentence invariant that makes cs-021's tally correct.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. What belongs in every column of a state table? Expected: every variable whose value the loop reads or writes, plus the loop condition
  1. Give the two classic off-by-one causes. Expected: boundary (≤ vs <) and initialisation (starting one early/late)
  1. When does a nested loop's inner count matter most? Expected: when estimating cost: it multiplies, so grid thinking exposes growth

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation