Skip to main content

Lesson 3.4: Nested Loops and Programs That Do Not Stop

A nested loop is a loop inside another loop. For every one repeat of the outer loop, the inner loop completes all of its repeats.

If an outer loop repeats 3 times and its inner loop repeats 4 times, the inner body runs 12 times. Multiply the counts when the limits do not change.

Nested loops can describe rows and columns. The outer loop can select a row while the inner loop processes each column in that row. Keep the two loop variables separate so the trace remains clear.

A program that does not stop often has a condition that never becomes false, a variable changing in the wrong direction, or a repeat count much larger than intended.

Guided practice

Build an outer repeat of 2 and an inner repeat of 3. Print scan in the inner body. Predict the number of output lines, then run and count them.

Add a movement to the inner body and a turn after the inner loop. Use Step to separate what happens per column from what happens per row. If the safety limit stops your program, inspect the smallest loop first.

Loading the block workspace...

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.