Science / Grades 4-7
Programming basics: repeat and debug
Trace fixed-count loops, identify the loop body, and debug missing or misplaced actions.
The video could not load. The transcript is still available below.
Next: your worksheet
Programming Basics: Repeat a Group of Steps
Checking sign-in...
Read the transcript
Write a repeated action once
Sometimes a program needs to do the same action several times. A loop lets us describe that repetition. Our plan repeats one drawing action three times, then performs a different action afterward. The key is knowing which instructions belong inside the loop.
Find the loop body
Read the instruction repeat three times, then look at its body: draw dot. Each repetition draws one dot. The word end marks where the repeated body finishes. It is a boundary in our teaching notation, not another drawing action.
Count the repetitions
Trace each pass. After the first pass there is one dot. After the second there are two. After the third there are three. The program does not need three copies of the draw instruction; it uses the same instruction on each pass.
Continue after the loop
After the end marker, the program says draw line. That instruction is outside the repeated body, so it happens once after the three dots. The line did not buy a ticket for the repeat ride. Its position in the plan matters.
Pause and change the count
Change only the repeat count from three to four. Leave the dot inside the loop and the line after it. Pause and predict both the number of dots and the number of lines. Do not multiply every instruction just because the program contains a loop.
Check the two counts
The revised plan makes four dots and still just one line. Only the loop body repeats. If we wanted a line on every pass, its instruction would need to move inside that body. A careful trace can catch a misplaced instruction.
Zero can be a useful count
Fun fact: repeating a body zero times can be perfectly meaningful. The body is skipped, but instructions after the loop still run. In our example, a repeat count of zero gives no dots and one line. Zero does not mean erase the whole program.
Predict, check, and improve
Debugging means finding and fixing mistakes in a program. Compare the intended result with the traced result, change one thing, and check again. Continue to the worksheet to practice loop boundaries and zero repetitions. No downloads or code execution are needed for this practice.