Coding with Loops, Conditions and Efficient Code

What your child is learning
Code is a precise list of instructions that a machine follows exactly as written, whether or not that was what the writer meant. In Grade 5, children read code, predict what it does, fix mistakes (debugging), and rewrite it so it does the same job in fewer, cleaner lines. All of this can be done on paper, with no screen, which is exactly how it is practised in many classrooms.
Four ideas come together:
- Sequence: steps run in order, top to bottom.
- Loops: "repeat 4 times" replaces writing the same steps again and again, and loops can sit inside other loops.
- Conditions: "if the next square is blocked, turn; otherwise move forward" lets code choose.
- Variables: a named value, like size, that can be changed in one place instead of everywhere.
This is the coding section of the Ontario Algebra strand. The central question this year is not just "does it work?" but "is it efficient?"
For a first look at these ideas, see the Grade 4 guide on coding with conditions.
Why it matters
Efficient code is easier to read, quicker to fix and less likely to hide a bug. Spotting that eight lines are really two lines repeated four times is the same pattern-finding skill your child uses in math, which is why coding sits beside patterns in the curriculum. It also teaches patience with precision: a computer cannot guess that "go to the door" means walking around the table.
Even children who never become programmers benefit from learning to break a big task into small exact steps and to test them one at a time.
How to teach it
1. Agree on a tiny language
On graph paper, a "pen" starts in one square facing the top of the page. The commands are forward n (draw n squares along a grid line), turn (a quarter turn clockwise, the way clock hands move), and repeat k [ ... ] (do what is inside the brackets k times).
2. Trace before you run
Trace this program by hand, step by step, drawing as you go:
forward 3
turn
forward 3
turn
forward 3
turn
forward 3
turn
This draws a square, 3 squares on each side, and ends where it began, facing the same way. That is eight lines. The same program with a loop:
repeat 4 [ forward 3, turn ]
Same picture, one line. Efficient code does the same job with less to write and less to get wrong.
3. Loops inside loops
Some pictures repeat a repeat. "repeat 3 [ repeat 4 [ forward 1 ], turn ]" moves 4 squares on each pass and does 3 passes: 12 squares in all. Reading from the inside out helps: what does the inner loop do? How many times does the outer loop run it?
4. Add a condition
Draw a path across the grid with some blocked squares. Write: "repeat 8 [ if the next square is blocked, turn; otherwise forward 1 ]". Trace it, and see how the same code reacts differently to different grids. Ask what happens if the path is completely open.
5. Use a variable
Write "set size to 2" at the top and use "repeat 4 [ forward size, turn ]". Changing the first line to "set size to 5" draws a bigger square without touching anything else. One change in one place is the payoff.
Common mistakes
- Counting the loop wrong. "Repeat 4 [ forward 3 ]" moves 12, not 3 or 4. Write the total next to the loop.
- Putting too much inside the brackets. A step that should happen only once (like a final turn) is placed inside the loop, so it happens four times. Mark the brackets clearly.
- Assuming the computer understands the plan. "Go around the table" means nothing without numbers. Ask: what is the exact first command?
- Shortening and changing the result. The new loop looks neat, but traces to a different picture. Always trace both versions and compare.
- Treating "if" as always true. A condition only runs its first branch when it is met. Test with a case where it is, and a case where it isn't.
Do this together
Code the drawing. Each of you draws a simple design on graph paper using only straight lines on the grid, perhaps a staircase, a plus sign or a letter. Without showing it, write the code to draw it, using the command set above. Swap papers and draw each other's picture from the code.
Now the efficiency round: look at the code you received and see if any steps repeat. Rewrite it with a repeat loop and count the lines you saved. Run it again to check the picture hasn't changed. The winner is the person whose code is shortest and still draws the picture exactly.
Printable worksheet
17 questions, a picture warm-up and drawing tasks, with an answer key on its own page. Print it or save it as a PDF.
Every worksheet is on the printable worksheets page.
Ontario curriculum: Grade 5, Algebra, expectation C3 · official document · U.S. Common Core crosswalk