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

Writing Efficient Code

C3.1 A1About 45 minutes
Learning goals
  • I can write and run code that uses loops, conditional statements and variables.
  • I can use IF ... ELSE IF ... ELSE to choose among more than two paths.
  • I can make my code more efficient by using loops, variables and REPEAT UNTIL.
  • I can write code to model a math situation, like a pattern, an equation or an inequality.

Words to know

efficient code
Code that does the job with fewer lines to write and fewer steps to run, and is easy to read and change.
loop
A control structure that repeats lines of code, like REPEAT 4 times or REPEAT UNTIL.
conditional statement
Code that makes a decision: IF a condition is true THEN do one thing, ELSE do another.
ELSE IF
Adds another condition to check when the first one is false. It lets code choose among three or more paths.
variable (in code)
A named box that holds a value, like t or total, which can change as the code runs.
execute
Run the code, one step at a time.

Remember from Grade 5

In Grade 5 you wrote and ran code with loops (REPEAT 6 times, REPEAT UNTIL), conditional statements (IF ... THEN ... ELSE) and variables (SET total = 0). You kept a tracking table to run code by hand.

In Grade 6 your code should also be efficient: it does the job without extra lines or extra steps. As before, you don't need a computer. You'll write code on paper and run it yourself.

Let's start with a drawing robot

Zara wants a robot to draw a square with sides of 5 steps. Here are two codes that both work.

MOVE 5
TURN RIGHT 90°
MOVE 5
TURN RIGHT 90°
MOVE 5
TURN RIGHT 90°
MOVE 5
TURN RIGHT 90°

Code A: 8 lines. The same two lines are written 4 times.

REPEAT 4 times
MOVE 5
TURN RIGHT 90°

Code B: 3 lines. The loop repeats the two indented lines 4 times.

start12345 steps a sideREPEAT 4 timesMOVE 5TURN RIGHT 90°Each trip through the loop draws one side(1, 2, 3, 4), then turns.
Code B in action. The robot starts facing right; each of the 4 trips through the loop draws one side and makes one right turn, so 3 lines draw the whole square.

Both draw the same square. Code B is more efficient: it is shorter, easier to read, and easier to change. To make the sides 8 steps long, you change one number in Code B, but four numbers in Code A.

Ways to make code efficient

  • Use a loop instead of copying the same lines.
  • Use a variable, so one change updates everything.
  • Stop early with REPEAT UNTIL as soon as you have the answer.
  • Skip what you don't need: to list multiples of 7, count by 7 instead of checking every number.
  • Count the lines you write and the steps the computer runs. Fewer is usually better, as long as the code still works!
Worked example

Example 1: Code that makes a linear pattern

Run this code. What pattern does it make? Write the pattern's equation.

SET t = 4

REPEAT 6 times

SAY t

ADD 3 to t

  1. Track t: it starts at 4. SAY 4, then t becomes 7.
  2. SAY 7, t becomes 10. SAY 10, t becomes 13. SAY 13, t becomes 16. SAY 16, t becomes 19. SAY 19, t becomes 22.
  3. The loop ran 6 times, so it said 6 numbers.
  4. The jump is 3 and the first term is 4, so the equation is t = 3n + 1.

4, 7, 10, 13, 16, 19: the toothpick-square pattern, t = 3n + 1. Four lines of code can make a pattern as long as you like: just change the 6.

Choosing among three paths: ELSE IF

IF ... ELSE picks between two paths. To choose among three or more, add ELSE IF. The computer checks the conditions in order and runs only the first one that is true.

IF age < 6 THEN
SAY "free"
ELSE IF age < 13 THEN
SAY "$5"
ELSE
SAY "$9"

Ticket prices for a winter festival skating show.

Worked example

Example 2: Run an ELSE IF

Run the ticket code for ages 4, 6, 12 and 13.

  1. age 4: Is 4 < 6? True. SAY "free". (The other checks are skipped.)
  2. age 6: Is 6 < 6? False. Is 6 < 13? True. SAY "$5".
  3. age 12: 12 < 6 is false. 12 < 13 is true. SAY "$5".
  4. age 13: 13 < 6 is false. 13 < 13 is false. So run ELSE: SAY "$9".

4 → free, 6 → $5, 12 → $5, 13 → $9. Only one path runs each time.

Worked example

Example 3: Code that solves an equation

Write efficient code to find n in 3n + 5 = 41. Run it.

  1. Start n at 0 and add 1 each time until 3 × n + 5 is 41. REPEAT UNTIL stops as soon as the answer is found.
  2. Code: SET n = 0, then REPEAT UNTIL 3 × n + 5 = 41 with ADD 1 to n inside, then SAY n after the loop.
  3. Run: n = 1 gives 8, n = 2 gives 11, ... n = 11 gives 38, n = 12 gives 41. Stop.
  4. Check by solving: 3n = 36, so n = 12 ✔.

It says 12. The loop stops at 12 instead of checking numbers all the way to 100.

Worked example

Example 4: Code that lists the solutions of an inequality

Write efficient code that says every whole-number solution of 6n + 10 < 70.

  1. The solutions start at 0 and stop at the boundary, so we can SAY each n until 6 × n + 10 is no longer less than 70.
  2. Code: SET n = 0, then REPEAT UNTIL 6 × n + 10 ≥ 70, and inside the loop SAY n and ADD 1 to n.
  3. Run: 0 (10), 1 (16), 2 (22), ..., 9 (64). When n = 10, 6 × 10 + 10 = 70, and 70 ≥ 70, so the loop stops.

It says 0, 1, 2, 3, 4, 5, 6, 7, 8, 9: the same solutions as in the inequalities lesson.

S★startgoalThe robot starts on S, facing right.
A grid 6 squares wide and 4 squares tall. The start square S is in the bottom-left corner. The goal star is in the top-right corner.
Worked example

Example 5: Shorter robot code

The robot starts on S, facing right. It needs to reach the ★. Write the code three ways and count the lines.

  1. To reach the ★, the robot moves 5 squares right and 3 squares up.
  2. Long way: MOVE 1 five times, TURN LEFT, MOVE 1 three times. That's 5 + 1 + 3 = 9 lines.
  3. With loops: REPEAT 5 times (MOVE 1), TURN LEFT, REPEAT 3 times (MOVE 1). That's 5 lines (each loop takes 2 lines).
  4. With numbers: MOVE 5, TURN LEFT, MOVE 3. That's 3 lines.

MOVE 5, TURN LEFT, MOVE 3 is the most efficient. We use TURN LEFT because a robot facing right that turns left ends up facing up.

Be an efficient coder

  • Before you write, look for repeats. Can a loop do them?
  • Use variables for numbers you might change.
  • Use REPEAT UNTIL when you don't know how many times to repeat.
  • Always run your code with a tracking table to check it works. An efficient code that gives the wrong answer is still a bug!
  • Bugs are normal. Every coder fixes bugs. Stay calm, trace one line at a time, and you'll find it.
Just for fun

Joke: Why did the robot end up in the next town instead of drawing a square?

Somebody forgot the TURN RIGHT 90° line. It's still going straight!

Hands-on activity

Floor-grid robot

You need: painter's tape or 24 sheets of paper to make a 6 by 4 grid on the floor, a small toy or a paper ★ for the goal, paper and a pencil.

  1. Make the grid. Put a start square in one corner and the ★ somewhere else.
  2. Write code for the robot (your grown-up) to reach the ★. Use only MOVE (number of squares), TURN LEFT and TURN RIGHT. The robot follows your code exactly.
  3. Count your lines. Can you make the code shorter with loops or bigger MOVE numbers? Rewrite it and test it again.
  4. Add an obstacle (a pillow on one square). Write new code that goes around it.
  5. Challenge: mark a 3 by 3 block of squares on the grid. Start the robot on a corner square of the block. Write code with a loop that makes the robot walk around the edge squares of the block and end where it started.

What to look for: code that works on the first or second try, and a second version with fewer lines. For step 5: REPEAT 4 times, MOVE 2, TURN RIGHT (or TURN LEFT, depending on the direction). Each side of the block is 3 squares, so the robot moves 2 squares to get from one corner square to the next.

Grown-up note: clear the floor so nobody trips, tape paper sheets down so they don't slide, and walk slowly as the robot.

Your turn

1

Make this code more efficient:

SAY "hip"

SAY "hip"

SAY "hip"

SAY "hooray!"

2

Run this code. What does it say?

SET t = 5

REPEAT 5 times

SAY t

ADD 4 to t

3

Run the ticket code (IF age < 6 ... ELSE IF age < 13 ... ELSE) for age 5 and age 30.

4

Look at the grid picture. Write the shortest code to get from S to the ★, using MOVE, TURN LEFT and TURN RIGHT. The robot starts facing right.

5

Write code that finds which term of t = 4n + 2 has the value 50.

6

In the square-drawing robot, Code A had 8 lines and Code B had 3 lines. To draw a square with sides of 9 instead of 5, how many numbers must you change in each code?

7

Write efficient code that says the even numbers from 2 to 20.

More practice: warm-up

1

What does REPEAT 4 times do?

2

Run: SET x = 3, then ADD 5 to x, then SAY x.

3

Which is more efficient: writing SAY "hi" 6 times, or REPEAT 6 times with SAY "hi" inside?

4

Run: IF 10 > 7 THEN SAY "yes" ELSE SAY "no"

5

What is efficient code?

More practice: on your own

1

Run this code for score = 95, 70 and 69.

IF score ≥ 90 THEN SAY "gold"

ELSE IF score ≥ 70 THEN SAY "silver"

ELSE SAY "bronze"

2

What does this code say?

SET total = 0

SET n = 1

REPEAT 5 times

ADD n to total

ADD 1 to n

SAY total

3

Write efficient code to find the first multiple of 7 greater than 50.

4

Write code that says every whole-number solution of 5n + 3 < 40.

5

Make this code more efficient:

MOVE 6

TURN RIGHT 120°

MOVE 6

TURN RIGHT 120°

MOVE 6

TURN RIGHT 120°

Challenge

1

What does this code say? Is the pattern linear?

SET a = 1

REPEAT 6 times

SAY a

MULTIPLY a by 2

2

Two codes list the solutions of 6n + 10 < 70. Code A sets n = 0 and repeats 101 times: IF 6 × n + 10 < 70 THEN SAY n, then ADD 1 to n. Code B is the REPEAT UNTIL code from Example 4. Do they say the same thing? Which is more efficient?

3

Kofi wrote this ticket code. What does it say for a 4-year-old? Fix the bug.

IF age < 13 THEN SAY "$5"

ELSE IF age < 6 THEN SAY "free"

ELSE SAY "$9"

Check your understanding

Quiz

1.

Which code is the most efficient way to draw a square with sides of 5?

2.

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

3.

IF age < 6 THEN "free" ELSE IF age < 13 THEN "$5" ELSE "$9". What does it say for age = 6?

4.

Why is REPEAT UNTIL a good choice for finding the first multiple of 9 greater than 100?

5.

SET total = 0. REPEAT 4 times: ADD 6 to total. SAY total. What does it say?

6.

Efficient code ...

For parents and teachers

Big idea: Grade 6 students solve problems and create computational representations of mathematical situations by writing and executing efficient code, including code with conditional statements and other control structures (C3.1). New this year: thinking about efficiency (fewer lines, fewer steps, easier to change) and choosing among three or more paths with ELSE IF. All coding here is unplugged: written on paper and run by hand.

Common difficulties:

  • Making code shorter but breaking it. Efficient code must still work. Always run it with a tracking table.
  • Not knowing what is inside a loop. Indented lines are inside; the first line that is not indented runs after the loop.
  • ELSE IF order. The computer stops at the first true condition, so checks must be in a sensible order.

How to help: Be the floor-grid robot and follow your child's code exactly, even when it's wrong. Ask, "Can you make it shorter?" If you have a computer or tablet, free block-coding sites such as Scratch have REPEAT, REPEAT UNTIL and IF ELSE blocks, but they are optional. Social-emotional learning (A1): debugging builds patience and perseverance; praise careful tracing.