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

Coding with Conditional Statements

C3.1 A1About 45 minutes
Learning goals
  • I can explain what a conditional statement (IF ... THEN ... ELSE) does.
  • I can write code that makes a decision using a comparison like > or <.
  • I can combine conditional statements with loops and variables.
  • I can write and run code on paper to solve a math problem.

Words to know

code
Instructions written for a computer or robot to follow.
condition
A question that is either true or false, like Is n > 5?
conditional statement
Code that makes a decision: IF the condition is true THEN do one thing, ELSE do something different.
control structure
Code that controls which lines run and how many times, like a loop or a conditional statement.
variable (in code)
A named box that holds a number, like score. Its value can change while the code runs.
execute
Run the code and follow the steps.

Remember from Grade 4

In Grade 4 you wrote code with sequential steps (in order), concurrent events (at the same time), loops (REPEAT 3 times) and nested loops (a loop inside a loop). You also used a variable in code, like SET total = 0 and ADD 2 to total.

In Grade 5 your code will make decisions. You don't need a computer: you will write code on paper and run it yourself, step by step, like a computer would.

Let's start with a decision

Every winter morning, Zoe's dad checks the temperature. He thinks: If it is colder than 0 degrees, wear a toque. Otherwise, a toque is optional. That's a conditional statement! Code can do the same thing.

SET temperature = (read the thermometer)
IF temperature < 0 THEN
SAY "Wear a toque!"
ELSE
SAY "A toque is optional."
SAY "Have a great day!"

A conditional statement. The indented lines belong to the IF or the ELSE.

How a conditional statement works

  1. The computer checks the condition (temperature < 0). It is either true or false.
  2. If it is true, it runs the lines under THEN and skips the ELSE part.
  3. If it is false, it skips the THEN part and runs the lines under ELSE.
  4. Either way, it carries on with the next line that is not indented: Have a great day!

Only one of the two paths runs each time.

Is number > 5?yesnoSAY "big"SAY "small"Only ONE path runs each time.
A decision diagram. The question 'Is number > 5?' splits into two paths: yes leads to SAY big, no leads to SAY small.
Worked example

Example 1: Run a conditional

Run this code when number = 3, then when number = 9.

IF number > 5 THEN SAY "big" ELSE SAY "small"

  1. number = 3: Is 3 > 5? False. Skip THEN, run ELSE: SAY "small".
  2. number = 9: Is 9 > 5? True. Run THEN: SAY "big".
  3. What about number = 5? Is 5 > 5? False, so it says "small". The condition decides!

3 → small. 9 → big. 5 → small.

Conditionals inside loops

Conditionals get really powerful inside a loop. The code below counts from 1 to 15. For each number, it makes a decision.

SET n = 1
REPEAT 15 times
IF n is a multiple of 3 THEN
CLAP
ELSE
SAY n
ADD 1 to n

A loop with a conditional statement inside. ADD 1 to n is inside the loop but not inside the IF or ELSE, so it runs every time.

Worked example

Example 2: Run a loop with a decision

What is the outcome of the code above? How many claps?

  1. n = 1: not a multiple of 3, SAY 1. Add 1, so n = 2.
  2. n = 2: SAY 2. n = 3: multiple of 3, CLAP.
  3. Keep going: 1, 2, clap, 4, 5, clap, 7, 8, clap, 10, 11, clap, 13, 14, clap.
  4. The claps happen at 3, 6, 9, 12 and 15.

The outcome is 1, 2, clap, 4, 5, clap, 7, 8, clap, 10, 11, clap, 13, 14, clap: 5 claps. The code made a pattern!

Worked example

Example 3: Write code to solve an inequality

Write code that finds all the whole-number solutions of 4 × n < 28 from 0 to 10.

  1. We need to test every n from 0 to 10. That's 11 numbers, so use a loop: REPEAT 11 times.
  2. Inside the loop, use a conditional: IF 4 × n < 28 THEN SAY n.
  3. Don't forget to change n each time: ADD 1 to n.
  4. Run it: n = 0 to 6 pass the test (0, 4, 8, ..., 24 are all < 28). n = 7 gives 28, which fails, and so do 8, 9, 10.

Code:

SET n = 0

REPEAT 11 times

    IF 4 × n < 28 THEN SAY n

    ADD 1 to n

Outcome: 0, 1, 2, 3, 4, 5, 6, the same solutions you found in the inequalities lesson.

Another control structure: REPEAT UNTIL

Sometimes you don't know how many times to repeat. REPEAT UNTIL keeps going until a condition becomes true.

SET savings = 0
SET weeks = 0
REPEAT UNTIL savings > 50
ADD 8 to savings
ADD 1 to weeks
SAY weeks

Liam saves $8 a week. The loop stops as soon as savings are more than $50.

SET savings = 0, weeks = 0ADD 8 to savingsADD 1 to weekssavings > 50?nogo aroundagainyesSAY weeks
A REPEAT UNTIL loop checks its condition after each time around. While savings > 50 is false, it loops back. When it becomes true (after 7 weeks), the loop stops and the code says the answer.
Worked example

Example 4: Run a REPEAT UNTIL loop

Run the savings code. What does it SAY?

  1. Use a table to keep track. After each time through the loop: savings 8, weeks 1. Then 16, 2. Then 24, 3. Then 32, 4. Then 40, 5. Then 48, 6.
  2. Is 48 > 50? No, so keep going: savings 56, weeks 7.
  3. Is 56 > 50? Yes. Stop the loop.

It says 7. Liam needs 7 weeks to have more than $50.

Be the computer

  • Keep a tracking table for every variable. Cross out the old value and write the new one.
  • At each IF, ask: true or false? Then follow only one path.
  • Use the indents to see which lines are inside the loop, inside the IF or inside the ELSE.
  • If your outcome looks strange, go back one step at a time. Finding a bug is a normal part of coding, and every coder does it!
Just for fun

The clap-on-multiples-of-3 code in Example 2 is a cousin of a real counting game called Fizz Buzz, which kids play in classrooms and coders write as practice. Try it at dinner: say fizz for multiples of 3 and see who forgets first!

REPEAT 10 times
TAKE the top card
IF the card's number > 5 THEN
PUT it on the RIGHT pile
ELSE
PUT it on the LEFT pile

The card-sorting code for the activity below.

Hands-on activity

Card-sorting robot

You need: a deck of playing cards with the face cards (jacks, queens, kings) taken out, and two sheets of paper labelled LEFT and RIGHT.

  1. Your grown-up reads the card-sorting code above, one line at a time. You are the robot. Follow it exactly! (An ace counts as 1.)
  2. When you finish, check: are all the cards on RIGHT 6 or more? Are all the cards on LEFT 5 or less?
  3. Now write your own code to sort 10 cards into red and black piles, or into even and odd piles. Your grown-up is the robot this time. Does your code work?
  4. Challenge: add a variable. SET count = 0 at the start, and ADD 1 to count every time a card goes on RIGHT. SAY count at the end.

What to look for: your child follows only one path for each card, and the code they write has a clear condition, a THEN and an ELSE.

Optional: if you have a computer or tablet, a free block-coding website such as Scratch has IF and IF ELSE blocks. Use it with a grown-up. It is not needed for this lesson.

Your turn

1

Run this code for age = 12:

IF age > 10 THEN SAY "ticket costs $8" ELSE SAY "ticket costs $5"

2

Run the same code for age = 10.

3

Write a conditional statement that says "even" if a number is even and "odd" if it is not.

4

What is the outcome?

SET n = 1

REPEAT 6 times

    IF n is even THEN SAY "❄️" ELSE SAY "🍁"

    ADD 1 to n

5

What does this code SAY?

SET total = 0

REPEAT UNTIL total > 20

    ADD 6 to total

SAY total

6

In the clapping code (Example 2), what would happen at n = 18 if it repeated 20 times?

7

Fill in the condition so the code says "snow day fun!" only when there is more than 15 cm of snow:

IF ☐ THEN SAY "snow day fun!"

More practice: warm-up

1

Is the condition 8 < 3 true or false?

2

Is the condition 20 > 19 true or false?

3

In an IF ... THEN ... ELSE statement, how many of the two paths run each time?

4

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

5

What does a REPEAT UNTIL loop do?

More practice: on your own

1

What is the outcome?

SET n = 1

REPEAT 10 times

    IF n > 7 THEN SAY n

    ADD 1 to n

2

What is the outcome?

SET n = 1

REPEAT 12 times

    IF n is a multiple of 4 THEN SAY "⭐"

    ADD 1 to n

3

Write code that SAYs every multiple of 5 from 1 to 30.

4

What does this code SAY?

SET coins = 100

SET days = 0

REPEAT UNTIL coins < 30

    SUBTRACT 15 from coins

    ADD 1 to days

SAY days

5

Write code to find the whole-number solutions of n + 9 > 15 for n from 0 to 10.

Challenge

1

What does this code SAY?

SET score = 0

SET n = 1

REPEAT 8 times

    IF n is even THEN ADD 5 to score

    ELSE ADD 1 to score

    ADD 1 to n

SAY score

2

Write code using REPEAT UNTIL that finds how many $6 tickets you can buy before you have spent more than $40. Then say the answer.

3

Write code that counts from 1 to 20 and SAYs "fizz" for multiples of 4, and the number otherwise. How many times does it say "fizz"?

Check your understanding

Quiz

1.

What does a conditional statement do?

2.

What does this say when n = 6? IF n < 5 THEN SAY "A" ELSE SAY "B"

3.

SET n = 1, REPEAT 10 times: IF n is a multiple of 5 THEN CLAP, then ADD 1 to n. How many claps?

4.

SET total = 0. REPEAT UNTIL total > 30: ADD 7 to total. SAY total. What does it say?

5.

Which condition is true when x = 12?

6.

Which is a control structure?

For parents and teachers

Big idea: Grade 5 students solve problems and create computational representations of math situations by writing and executing code, including code with conditional statements and other control structures (C3.1). Conditionals (IF ... THEN ... ELSE) are new this year; loops and nested loops were taught in Grades 3 and 4. All of this lesson is unplugged: the code is written on paper in plain words, and your child runs it by hand. No software is needed.

Common difficulties:

  • Running both THEN and ELSE. Stress that only one path runs each time.
  • Forgetting to change the counter (ADD 1 to n), so the code keeps checking the same number (and a REPEAT UNTIL loop could run forever).
  • Mixing up < and >, especially at the boundary (is 5 > 5? No).

How to help: Be the robot for your child's code, and follow it exactly, even when it's wrong. It's a fun way to find bugs. Everyday decisions make good practice: "IF it's raining THEN take an umbrella ELSE wear sunglasses." If you'd like to try a computer, free block-coding tools like Scratch have IF ELSE blocks, but they are optional. Social-emotional learning (A1): debugging takes patience; praise careful tracing and trying again.