Skip to content
KinPilot™Learn & Explore
Menu

Grades 3–5 · Computing

How can a first mismatch locate an instruction bug?

A bug is a mismatch between intended behavior and what instructions actually produce. The first mismatch shows where a sequence begins to go wrong. This means comparing each resulting state, not just the final square.

D09 visual explanationThe five-by-five grid counts columns from the left and rows from the top. Start in column 2, row 4 facing N (page-up). F moves one square in the current facing; L turns 90 degrees anticlockwise in place, R turns 90 degrees clockwise in place. The intended state trace follows F,F,L,F. Given commands: F,F,R,F. The first position-or-facing mismatch is after command 3. Replace R at that step with L, reset to the start, and rerun all commands. The given endpoint is column 3, row 2, facing E.Debug the first position-or-facing mismatchStart: column 2, row 4, facing N (page-up)1234512345Goal: (1,2), facing WPlanGiven1FF2FF3LR4FFFirst mismatch: step 3Repair: R → LF moves; L/R turn 90° in place. No sideways moves.

In the diagramDebug the first position-or-facing mismatchStart: column 2, row 4, facing N (page-up)12345Goal: (1,2), facing WPlanGivenFLRFirst mismatch: step 3Repair: R → LF moves; L/R turn 90° in place. No sideways moves.

The first mismatch occurs after the third command: R faces the bot east instead of the planned west. The differing facing then changes the final forward movement.

The bot's state includes its position and facing direction. It starts in column 2, row 4 facing north, which means page-up. F moves one square forward in the current direction. L and R turn a quarter-turn in place, leaving the position unchanged.

The intended sequence is F, F, L, F. The given sequence is F, F, R, F. Their first two forward movements agree. The third commands leave the bot on the same square but facing opposite ways: west in the plan and east in the given program.

That facing difference explains why the final forward movement goes the wrong way. Replacing the third R with L restores the intended sequence. Restarting from the original state makes the comparison consistent. A matching endpoint alone would not establish agreement with every intermediate position and facing.

About this explanation

Starting knowledge: Read row and column locations and recognize a quarter-turn while keeping track of a facing direction.

Sources

  • University of Canterbury, CS Unplugged — Kidbots: Exact commands, testing and debugging
← Grades 3–5 topics