Skip to content

Course navigation

For teachersTopic 2.12 · Lesson kit

Informal Run-Time Analysis

  • 2 class periods
  • 45 minutes each
  • Objective 2.12.A
  • 6 downloads with keys

Downloads

At a glance

Pacing
2 class periods of 45 minutes
Learning objectives
2.12.A
Essential knowledge
2.12.A.1
Materials
Projector and the Count Every Step slide deckWhiteboard and markersMini whiteboards or scrap paperIDE on every computer (Day 1)Printed Count Every Step puzzle worksheet (answer key for you) and exit ticket

45 minutes a day

Agenda

Day 145 minutes

  • Warm-up
  • Direct instruction
  • Guided practice
  • Closure
  • Activity
  1. 0:00–0:055 minWarm-upwho claps more? (slide 2)
  2. 0:05–0:1712 minDirect instructionexecution counts and the parts of a for loop (slides 3–6)
  3. 0:17–0:225 minActivityDay 1 pause and try — doubling up (slide 7)
  4. 0:22–0:3412 minDirect instructionreading the header, steps other than 1 (slides 8–11)
  5. 0:34–0:428 minGuided practicepredict header counts, then check with a counter
  6. 0:42–0:453 minClosureone pass too many

Day 245 minutes

  • Warm-up
  • Direct instruction
  • Closure
  • Exit ticket
  • Activity
  1. 0:00–0:055 minWarm-uphow many times does each line run?
  2. 0:05–0:1712 minDirect instructionifs and nested loops (slides 12–15)
  3. 0:17–0:225 minActivityDay 2 pause and try — the inner loop gets shorter (slide 16)
  4. 0:22–0:3210 minDirect instructioncomparing run times (slides 17–19)
  5. 0:32–0:375 minActivityDay 2 pause and try — predict the bigger input (slide 20)
  6. 0:37–0:403 minClosuremultiply or add? (slides 21–22)
  7. 0:40–0:455 minExit ticket

Overview

A statement’s execution count is how many times it runs while the program runs (2.12.A.1). Day 1 is counting: tracing a loop to count its statements, and reading a for or while header to know how many times the body runs, including steps other than 1 and loops that never run. Day 2 is structure: an if inside a loop gives two counts, nested loops multiply, and informally comparing two pieces of code by how many statements each executes and how that count grows when the input grows.

Stay inside the boundary: counts are numbers for given inputs. Do not introduce Big-O notation, formulas for sums, or logarithms by name (the halving loop is only “count the passes”).

Teacher note

The slides are the Count Every Step deck (22 slides); slide numbers refer to it. Day 1 uses slides 2–11, Day 2 slides 12–22. The worksheet (10 questions, about 35 minutes) is homework: questions 1, 2, 3, and 4 after Day 1; questions 5 to 10 after Day 2. Question 9 settles the Think First guess about doubling the input.

Demonstrations are in the topic’s examples/ folder (.snippet.java files hold only the statements for main): LapCount, StepTotal, HeaderCounts, ByFiveCount, OffByOne, NeverRuns (Day 1); EvenOdd, SeatingChart, NoBraces, NestedLess, ShortButBusy, SearchSteps, GrowthTable, Halving (Day 2).

Day 1 · Warm-up: Who claps more?

Show slide 2. Students predict which of the two claps more without running anything, and say what they would count to decide. Record the word “count” on the board; the lesson is about counting statement executions instead of timing a program.

Day 1 · Direct instruction: Counting executions

Slide 5: trace a short loop and write how many times each statement runs. Slide 6: in a for loop, the initialization runs once, the condition one time more than the body (the last check is false), and the update as often as the body. Run LapCount or StepTotal so students can confirm counts with a counter variable.

Misconception: Assuming every loop body runs at least once

A while or for loop checks its condition first. Run NeverRuns: score starts at 120, the condition score < 100 is false at once, and the body runs 0 times. The condition was still checked once.

Day 1 pause and try

Slide 7: students count the executions in the doubling code and write the final values before revealing.

Day 1 · Direct instruction: Reading the header

Slide 9: for count-by-1 headers, count the values the variable takes: k = 0; k < 12 is 12 values; k = 1; k <= 12 is 12 values; k = 4; k <= 12 is 9. Slide 10: for other steps, list the values (k = 0; k < 12; k += 3 gives 0, 3, 6, 9, so 4 passes). Slide 11: check both ends.

Misconception: Off-by-one counts

Students mix up < with <= or forget the first value, so the count is one too many or too few. Have them write the first value and the last value that still passes the condition, then count the list. Run OffByOne.

Day 1 · Guided practice: Guided practice

Show four headers from worksheet question 2 on the board. Students write the number of passes on mini whiteboards, then run HeaderCounts (or their own loops with a counter) to check.

Day 1 · Independent practice: Independent practice

Puzzle worksheet questions 1, 2, 3, and 4 are homework: the loop that counts down by 7, the header table, the code tiles that show the condition checked one more time than the body runs, and the snippet pool for counts 8, 5, and 12.

Day 1 · Closure: Closure

Cold-call: “How many times does the body run for for (int k = 12; k > 0; k -= 5)?” (3: k is 12, 7, 2.)

Day 2 · Warm-up: Warm-up

Show for (int k = 1; k <= 6; k++) { if (k % 2 == 0) { evens++; } } on the board. Students say how many times the condition k % 2 == 0 is evaluated (6) and how many times evens++ runs (3).

Day 2 · Direct instruction: Ifs and nested loops

Slide 13: an if in a loop has two counts: the test runs on every pass, the guarded statement only when the condition is true. Slide 14: nested loops multiply (the inner body runs outer passes times inner passes). Slide 15: loops one after another add. Run SeatingChart (12 stars, 3 println calls) and NestedLess (10, from 0 + 1 + 2 + 3 + 4).

Misconception: Adding instead of multiplying, or trusting the indentation

For a seating chart with 3 rows of 4 seats, the common answer is 3 + 4 = 7. Adding fits loops one after another; a loop inside a loop gives 3 times 4 = 12. Separately, a loop without braces repeats only the one statement after its header however it is indented; run NoBraces (cheers is 1).

Day 2 pause and try

Slide 16: the inner loop gets shorter; students add the inner passes for each outer pass (for example 0, 1, 2, 3, 4) rather than multiplying.

Day 2 · Direct instruction: Comparing run times

Slide 18: two segments reach the same answer, one checks every letter and the other stops when it finds a match; compare counts. Slide 19: when n doubles, a single loop does twice as many passes and a nested loop four times as many. Run GrowthTable and ShortButBusy to show that short code can do a lot of work (10,000 passes).

Misconception: Judging speed by how long the code looks

Fewer lines does not mean less work. ShortButBusy is five lines and runs its inner body 10,000 times. Counting executions, not reading length, compares two pieces of code.

Day 2 pause and try

Slide 20: students predict the work for the bigger input before the table is revealed, then explain the growth in one sentence.

Day 2 · Independent practice: Independent practice

Puzzle worksheet questions 5 to 10 are homework (the count match-up, the condition-versus-body face-off, which segment does less work, the one-token fix, two truths and a lie, and the wrap-up).

Day 2 · Closure: Closure and exit ticket

Show slides 21 (vocabulary) and 22: partners explain how to count the inner body of a nested loop. Give the exit ticket (about five minutes). Question 1 checks counting a nested loop whose inner bound depends on the outer variable, question 2 reading a header with a step, and question 3 counting several statements in nested loops (2.12.A.1).

Exit ticket

Question 1 · Tracing

Write the exact output.

int count = 0;
for (int a = 0; a < 5; a++)
{
    for (int b = 0; b < a; b++)
    {
        count++;
    }
}
System.out.println(count);
Check your reasoning
10

The inner body runs a times on each outer pass: 0, 1, 2, 3, and 4 times, and 0 + 1 + 2 + 3 + 4 = 10. It is not 5 times 5, because the inner loop gets shorter. A complete answer has 10 and shows the per-pass counts.

Question 2 · Multiple choice

How many times does the body of this loop run?

for (int k = 12; k > 0; k -= 5)

  1. 2
  2. 3
  3. 4
  4. 12
Check your reasoning

B. k takes the values 12, 7, and 2; then k is -3 and the condition is false. A stops too soon (12 and 7 only). C counts the failing value -3 or assumes k hits 0. D is the number of values counted by 1, but the step is 5.

Question 3 · Short answer

How many times does each of these run: seatsDone++;, rowsDone++;, and the System.out.println(); inside the outer loop? Explain how you know.

int seatsDone = 0;
int rowsDone = 0;
for (int row = 1; row <= 3; row++)
{
    for (int seat = 1; seat <= 4; seat++)
    {
        System.out.print("*");
        seatsDone++;
    }
    System.out.println();
    rowsDone++;
}
System.out.println("Seats printed: " + seatsDone);
System.out.println("Rows finished: " + rowsDone);
Check your reasoning

Model answer: seatsDone++; runs 12 times, and rowsDone++; and System.out.println(); each run 3 times. The inner body runs 4 times for each of the 3 outer passes, so 3 times 4 is 12. The two statements after the inner loop belong to the outer body, so they run once per outer pass, 3 times.

A complete answer gives 12, 3, and 3, and explains multiplying for the inner body but not for statements in the outer body.