Code Crew G2 / Week 04 / Thursday
4/6
Week 04 · Loop Logbooks

Thursday

Off by one
// Track a box through every lap
⏱ about 15 min
Rocket the inventor holding his engineering tablet

Count the laps carefully!

Rocket wanted the steps box to reach 5.

But his loop stopped at 4.

Rocket laughs. He counted one lap short!

Coders call this an off by one bug.

Even grown-up coders make this bug.

Raven says it is a great bug to hunt.

WARM-UP: THE BOX COUNTS LAPS
  • Peek in the box.
  • Tap how many laps.
set steps to 3
repeat steps times:
clap wings
?
set steps to 1
change steps by 1
repeat steps times:
flash lights
?
Rocket program with a repeat 4 times line

Four or five?

Rocket wants steps to reach 5.

But his loop runs only 4 laps.

Steps stops at 4. One short!

set steps to 0
repeat 4 times:
change steps by 1
move right
stop
say steps
A lap logbook where steps ends on 4, not 5

Off by one

Off by one means one too few.

Or it means one too many.

Count the laps to find it.

first lineloop lineends on 5?
set steps to 0repeat 4 times:?
set steps to 0repeat 5 times:?
set steps to 1repeat 4 times:?
set steps to 1repeat 5 times:?
WHERE DOES STEPS END?
  • Count every lap.
  • Tap the answer.
set steps to 1
repeat 4 times:
change steps by 1
?
set steps to 2
repeat 3 times:
change steps by 1
stop
say steps
?
set steps to 1
repeat 5 times:
change steps by 1
?

Fix the flight a new way.

Steps starts at 1 this time.

Nova must say 5.

BUILD A FLIGHT THAT ENDS ON 5
  • Start at 1, loop 4 laps.
  • Stop the loop, then say steps.
1line 1
2line 2
3line 3
4line 4
5line 5
A test flight logbook: height drops on the lap where steps holds 5

The flight logbook

Look at the height column.

It stays at 3, then drops to 1.

The drop happens on lap 5.

Steps holds 5 right then!

cluetrue?
height drops on lap 5?
steps holds 5 at the drop?
height drops on lap 2?
height goes up at the drop?

Off by one, fixed!

Off by one: one lap too few or many.

Count laps and check the start.

The drop came when steps held 5.

Great detective work, Crew!

← Wednesday