Math Bonk! Learning Centre Play the game

Coding with Nested Events

A toy train set

What your child is learning

A program stamps four dots in a row, drops to the next line, and does the same thing three times. It is repeating a repetition. A loop placed inside another loop is called a nested event, and reading it correctly is the new layer in Grade 6 coding.

Children learn to:

Loops, "if" choices and tidy code were the Grade 5 focus, in Coding with Loops, Conditions and Efficient Code. The same nesting idea returns with named pieces of code in the Grade 7 guide Coding with Nested Events and Subprograms.

A loop inside a loop: the outer loop runs 3 times and the inner loop 4 times each, so 12 stamps are printed.A loop inside a loop: the outer loop runs 3 times and the inner loop 4 times each, so 12 stamps are printed.inner loop: 4 timesouter loop: 3 times3 × 4 = 12 stamps in total
A loop inside a loop: the outer loop runs 3 times and the inner loop 4 times each, so 12 stamps are printed.

Why it matters

Rows and columns are everywhere in computing: a spreadsheet, a photo made of pixels, a seating plan. Each of them is naturally described as an outer loop for the rows and an inner loop for the cells in a row. A child who can read a nested loop can read the structure behind most of the screens in their life.

Predicting before running builds a habit that transfers far beyond coding. Careful reading, a written guess and an honest comparison with the result is how good debugging works, and it is also how a careful student checks any answer.

How to teach it

1. Say it in plain words first

"Put four books on a shelf, and do that on three shelves." Four repeated, three times, is twelve books. Only after the words make sense should brackets appear.

2. Predict, then run

repeat 3 [ repeat 4 [ stamp ] ]

Ask how many stamps this makes and write the guess down. Plenty of children say 7, by adding. Running it shows 12: four stamps on each of three passes, so the counts multiply.

3. Trace with a table

Outer pass 1: 4 stamps
Outer pass 2: 8 stamps in all
Outer pass 3: 12 stamps in all

A trace turns an invisible process into numbers. Your child fills in one row per pass of the outer loop and sees the total grow by the same amount each time, which is a growing pattern in disguise.

4. Draw it as a grid

Let the outer loop stand for rows and the inner loop for stamps along one row. Changing the inner count makes the picture wider. Changing the outer count makes it taller. The same idea produces a rectangle of any size from two numbers.

5. Watch where the line break goes

A "new line" command that sits inside the inner brackets puts every stamp on its own row. It belongs between the two closing brackets, so that it runs once per row. The other kinds of event show up here too: two sprites moving at once is a concurrent event, and one command finishing before the next begins is a sequential event.

6. Tidy the program

Fifteen lines that each say "stamp" do the same work as repeat 15 [ stamp ]. Reading for repeated lines and folding them into a loop is what efficient code means at this age.

Common mistakes

Do this together

Bingo-dabber grids. One of you is the outer loop and says "new row" each time. The other is the inner loop and dabs across a row of a blank grid with a bingo dabber, four dabs to a row. Run the program for three rows, then swap roles and change the rule to five dabs on two rows.

Finish by writing each version in the bracket notation used above. Compare the two programs and ask which single number had to change to make a taller picture, and which one made a wider one.

Printable worksheet

19 questions, a picture warm-up and drawing tasks, with an answer key on its own page. Print it or save it as a PDF.

Open the worksheet

Every worksheet is on the printable worksheets page.

Ontario curriculum: Grade 6, Algebra, expectation C3 · official document · U.S. Common Core crosswalk