Problem Solving — CS & DS

Lecture 06 — Traces and Loop Reasoning

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

Trace the tally: loop reasoning on paper

cs-021

Sum the numbers 1..12, skipping multiples of 3. Predict first, then trace the loop table.

Expected: 48

Cost: O(limit). Skipped items cost a condition check but no addition - skipping is not free.

Worked example

Nested loops, traced honestly

cs-023

With n = 4 rows and the rule 'star when column <= row else dot', what does the double loop print? Trace row 2 by hand.

count = 0
for row in range(4):
    for col in range(3):
        count += 1
print(count)

Expected: four lines: * then **. then ***.. then ****...

Cost: O(n^2) characters - the sum 1 + 3 + 5 + ... + (2n - 1) = n^2 is the doubling-grid fact from the lecture.

Case attempt · lecture 06
Case cs-021 · Beginner

Trace the Tally

A short accumulator loop with a twist (skip multiples of 3). Predict output by trace table, then verify.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-021 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-021 — not embedded in this deck.

Case attempt · lecture 06
Case cs-022 · Foundational

The Shrinking Loop

A loop divides a number while it is even. How many iterations for a given input, and when does it terminate? Build the trace and the general rule.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-022 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-022 — not embedded in this deck.

Case attempt · lecture 06
Case cs-023 · Intermediate

Nested and Confused

Two nested loops with a conditional print. Predict the exact output pattern; then generalise to n lines. Pure trace discipline.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-023 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-023 — not embedded in this deck.

Case attempt · lecture 06
Case cs-024 · Advanced

The Off-By-One Museum

Three near-identical loops that each misbehave by one step. Diagnose each, fix minimally, and state the lesson each artefact teaches.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-024 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-024 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What belongs in every column of a state table?
  2. Give the two classic off-by-one causes.
  3. When does a nested loop's inner count matter most?
Expected answers (instructor): every variable whose value the loop reads or writes, plus the loop condition boundary (≤ vs <) and initialisation (starting one early/late) when estimating cost: it multiplies, so grid thinking exposes growth
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF