Grade by Grade
C. Algebra · Patterns, Integers, Equations, Inequalities, Coding and Modelling

Coding with Counts and Sub-programs

C3.1 A1About 50 minutes
Learning goals
  • I can write and run code with a counter, and make something happen when the count reaches a certain number.
  • I can create a sub-program and call it by its name, with or without an input.
  • I can use sub-programs and loops to make my code more efficient.
  • I can write code that represents a math situation, like a pattern or a savings plan.

Words to know

counter
A variable that keeps count, like lap. It usually starts at 0 and goes up by 1 each time something happens.
defined count
A set number that a count must reach before something happens, like "after 12 laps" or "every 4th lap".
event
Something that happens in a program, like the computer saying a message or a robot turning.
sub-program
A named group of instructions that you define once and can use (call) as many times as you want. Some coding apps call it a procedure, a function or "My Blocks".
call
Use a sub-program by writing its name, like SQUARE(4).
input (parameter)
A value you give a sub-program when you call it, like the 4 in SQUARE(4).
efficient code
Code that does the job with fewer lines and fewer steps, and is easy to read and change.

Remember from Grade 6

In Grade 6 you wrote efficient code with loops (REPEAT 4 times, REPEAT UNTIL), variables (SET t = 4), and conditional statements with ELSE IF. You ran code by hand with a tracking table.

In Grade 7 you'll add two new tools: counters that make events happen at a defined count, and sub-programs that you define once and reuse. As always, no computer is needed: write the code on paper and run it yourself, one line at a time.

Let's start at hockey practice

The coach wants the team to skate 12 laps, take a water break every 4 laps, and hear "Practice over" at the end. Here is code for it. The variable lap is a counter.

SET lap = 0
REPEAT 12 times
ADD 1 to lap
IF lap is a multiple of 4 THEN
SAY "Water break!"
SAY "Practice over"

Lines that are indented (pushed to the right) belong to the loop or the IF above them.

Events and counts

The counter lap goes 1, 2, 3, ... 12. The event (saying "Water break!") happens only when the count is 4, 8 or 12. That's an event influenced by a defined count.

Counts can control events in different ways:

  • Every nth time: IF count is a multiple of 4 THEN ...
  • Exactly once, at a certain count: IF count = 10 THEN ...
  • Stop at a count: REPEAT UNTIL count = 20, or REPEAT 12 times.
1234Waterbreak!5678Waterbreak!9101112Waterbreak!lap = 1, 2, 3, ... 12. The IF is true only when lap is a multiple of 4: 4, 8 and 12.
The counter goes up by 1 every lap, but the event happens only at the defined counts: 3 water breaks, after laps 4, 8 and 12.

Sub-programs

A sub-program is like a recipe card with a name. You define it once. Then you call it by its name whenever you need it. Defining a sub-program doesn't run it. It only runs when it is called.

A sub-program can have an input. Here, size is an input: SQUARE(2) draws a small square and SQUARE(4) draws a bigger one, using the same instructions.

DEFINE SQUARE(size)
REPEAT 4 times
MOVE size
TURN RIGHT 90°
END DEFINE
SQUARE(2)
TURN RIGHT 90°
MOVE 3
TURN LEFT 90°
SQUARE(4)

The robot starts at the dot, facing up. MOVE moves forward that many grid squares.

start
The robot draws a 2-by-2 square, moves 3 squares to the right, then draws a 4-by-4 square.
Worked example

Example 1: Run the hockey code

Run the hockey practice code. What does it say, and how many water breaks are there?

  1. Make a tracking table. lap = 1: 1 is not a multiple of 4. lap = 2, 3: no.
  2. lap = 4: multiple of 4, so SAY "Water break!"
  3. lap = 8: SAY "Water break!" lap = 12: SAY "Water break!"
  4. The loop has run 12 times, so it ends. Then SAY "Practice over".

It says "Water break!" 3 times (after laps 4, 8 and 12), then "Practice over".

Worked example

Example 2: Why sub-programs are efficient

Count the lines needed to draw the two squares without a sub-program and without loops, and with the SQUARE sub-program.

  1. Without: each square needs MOVE and TURN written 4 times = 8 lines. Two squares = 16 lines, plus 3 lines to move between them = 19 lines.
  2. With the sub-program: 4 lines to define it (plus END DEFINE), and 5 lines in the main program = 10 lines.
  3. To draw a third square, the sub-program version needs only a few more lines. The long version needs 8 more each time.
  4. And if there's a mistake in how a square is drawn, you fix it in one place.

The sub-program version is shorter, easier to read and easier to fix: it is more efficient.

Worked example

Example 3: Code for a savings plan

Maya has $35 and saves $12.50 a week. Write code that finds how many weeks it takes to reach $160. Run it.

  1. Use a counter, week, and stop with REPEAT UNTIL as soon as the goal is reached.
  2. Code: SET savings = 35, SET week = 0, REPEAT UNTIL savings ≥ 160, with ADD 12.50 to savings and ADD 1 to week inside the loop, then SAY week after the loop.
  3. Run: week 1 → 47.50, week 2 → 60, week 3 → 72.50, ... week 9 → 147.50, week 10 → 160. 160 ≥ 160, so stop.
  4. Check with algebra (from the pattern rules lesson): 12.5w + 35 = 160 gives w = 10 ✓.

It says 10. The code and the equation agree.

Worked example

Example 4: A sub-program that makes patterns

Run this code.

DEFINE PATTERN(start, jump, terms)

SET t = start

REPEAT terms times

SAY t

ADD jump to t

END DEFINE

PATTERN(4, 3, 5)

PATTERN(2.75, 0.75, 4)

  1. PATTERN(4, 3, 5): start = 4, jump = 3, terms = 5. It says 4, 7, 10, 13, 16.
  2. PATTERN(2.75, 0.75, 4): start = 2.75, jump = 0.75, terms = 4. It says 2.75, 3.5, 4.25, 5.
  3. The same sub-program makes any linear pattern. Only the inputs change.

4, 7, 10, 13, 16, then 2.75, 3.5, 4.25, 5.

Run code like a computer

Make a tracking table with a column for each variable and a column for what the code SAYS or DRAWS. Go line by line. When you meet a sub-program call, jump to its DEFINE, run it with the inputs, then come back to the line after the call.

Just for fun

Programmers have a famous counting game called FizzBuzz: say "Fizz" for multiples of 3 and "Buzz" for multiples of 5. This lesson's Challenge plays the Canadian version, "Maple Leaf"!

Hands-on activity

Program a grown-up robot

You need: paper, a pencil and a family member to be the robot. Clear a safe space with no tripping hazards.

  1. Define a sub-program on paper, for example:

DEFINE CELEBRATE

JUMP

CLAP 2 times

END DEFINE

  1. Write a main program with a counter, like: SET step = 0, then REPEAT 10 times: ADD 1 to step, TAKE 1 STEP forward, IF step is a multiple of 5 THEN CELEBRATE.
  2. Predict what will happen (2 celebrations, after steps 5 and 10). Then let your robot run the code exactly as written.
  3. Change one thing (the count, the multiple, or what CELEBRATE does) and predict again.
  4. Count your lines. How many lines would you need without the sub-program?

Your turn

1

Run: SET count = 0, REPEAT 10 times: ADD 1 to count, IF count is a multiple of 3 THEN SAY count. What does it say?

2

DEFINE TRIANGLE(size): REPEAT 3 times: MOVE size, TURN RIGHT 120°. What does TRIANGLE(5) draw?

3

Write code that says 5, 4, 3, 2, 1 and then "Go!".

4

Using the PATTERN sub-program from Example 4, what does PATTERN(1.5, 0.5, 5) say?

5

How many times does SAY run? REPEAT 4 times: REPEAT 3 times: SAY "hi"

6

Use the SQUARE sub-program and a loop to draw squares of size 1, 2 and 3, all starting from the same corner.

More practice: warm-up

1

What is count after: SET count = 0, REPEAT 7 times: ADD 1 to count?

2

In the hockey code, does anything extra happen at lap 6?

3

Does writing DEFINE SQUARE(size) draw a square?

4

What is the input in SQUARE(7)?

5

List the multiples of 4 from 1 to 20.

More practice: on your own

1

Write code for 20 laps that says "Halfway!" after lap 10 and "Done!" after lap 20.

2

Run: SET total = 0, SET n = 1, REPEAT 5 times: ADD n to total, ADD 1 to n. Then SAY total.

3

Write a sub-program COST(students) that says the cost of a trip: $180 plus $12.50 per student. What does it say for COST(20) and COST(30)?

4

Run: SET t = 100, SET count = 0, REPEAT UNTIL t < 60: SUBTRACT 7.5 from t, ADD 1 to count. Then SAY count and SAY t.

5

Starting at the dot facing up, what does this draw? SQUARE(3), TURN RIGHT 90°, MOVE 3, TURN LEFT 90°, SQUARE(3)

Challenge

1

Write code that counts from 1 to 15. For multiples of 3 it says "Maple", for multiples of 5 it says "Leaf", for multiples of both it says "Maple Leaf", and otherwise it says the number. What is its output?

2

Write a sub-program STAIRS(steps) that draws a staircase going up and to the right, with each step 1 square high and 1 square wide.

3

Amir has $5 and adds $5 each week. Lily has $0.01 and her amount doubles each week. Write code that finds the week when Lily first has more than Amir. Run it.

Check your understanding

Quiz

1.

SET c = 0, REPEAT 20 times: ADD 1 to c, IF c is a multiple of 5 THEN SAY "Beep". How many times does it say "Beep"?

2.

What is a sub-program?

3.

Using DEFINE SQUARE(size) from the lesson, what does SQUARE(6) draw?

4.

Why can a sub-program make code more efficient?

5.

SET t = 2, REPEAT 4 times: SAY t, ADD 2.5 to t. What does it say?

6.

A counter starts at 0 and goes up by 1 each lap. Which line makes something happen only once, at lap 8?

For parents and teachers

Big idea: Grade 7 students solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code that involves events influenced by a defined count and/or sub-programs and other control structures (C3.1). A sub-program (called a procedure, function or "My Block" in some coding apps) is defined once and called by name. All the coding here is unplugged: on paper, run by hand.

Common difficulties:

  • Thinking DEFINE runs the code. It only runs when called.
  • Losing track of the counter. A tracking table with a column for each variable fixes this.
  • Indentation: which lines are inside the loop? Have your child draw a bracket beside the indented lines.
  • Order of ELSE IF tests (Challenge 1): the most specific test must come first.

How to help: Be the robot! Follow your child's code exactly as written, even when it's wrong, so they can find the bug themselves. If your family uses a free block-coding app, the same ideas appear there, but it isn't needed. This lesson builds A1 skills: perseverance while debugging, and clear communication.