Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies · Design and Make: Grade 5 Designers, Testers and Coders

Code That Senses: Forever Loops, If and Else

BC ADST 5: use familiar tools BC ADST 5: choose technologiesAbout 45 minutes
Learning goals
  • I can name the input, process and output in a program.
  • I can use a sensor reading, a forever loop and if-else so a program reacts on its own.
  • I can trace a program with different sensor values and find and fix a bug.

Words to know

sensor
A part that measures something in the world, such as temperature, light or motion, and sends it to a program.
input
Information that goes into a program, such as a sensor reading or a button press.
output
What a program does or shows, such as text on a screen, a light or a sound.
if-else
A choice in code: if the condition is true, do one thing; otherwise (else), do something different.
condition
A question in code that is either true or false, such as "is temp less than -20?"

Programs that notice the world

You have used loops, events, if-then and variables. Now your code will read a sensor and decide what to do by itself. Many small coding boards used in schools have a built-in temperature sensor.

Programs like this all have three parts:

  • Input: the sensor measures something.
  • Process: the code checks a condition and decides.
  • Output: the board shows, lights up or beeps.
INPUTtemperaturesensorPROCESSis temp lessthan -20?OUTPUTscreen showsWARM WATER or OKInside a forever loop, this happens once a second.
Input, process and output in Room 5's outdoor-day helper.

The outdoor-day helper

Room 5 found that on the coldest days, even the pouch could not keep plain cold water from freezing. So they made a rule for their project (a made-up rule, not a real school rule): at −20 °C or colder, Grade 2s should start with warm water in their bottles. They put a coding board by the back door to remind everyone.

Remember how negative temperatures work: −25 is colder than −20, so −25 is the smaller number. On a number line, colder temperatures are farther to the left.

-30-25-20-15-10-50510
−25 is less than −20 (farther left). −12 is greater than −20 (farther right).
on start
show "HELLO"
forever
set temp to (temperature in °C)
if temp < -20 then
show "WARM WATER"
else
show "OK"
pause 1 second

Version 1 of the outdoor-day helper. It checks the temperature every second.

Sensor readsIs temp < −20?Screen shows
−25 °CYesWARM WATER
−21 °CYesWARM WATER
−20 °CNoOK
−12 °CNoOK
Tracing Version 1. Look closely at −20: it is not less than itself.
Worked example

Finding the bug

The rule says −20 °C or colder means warm water. One morning the sensor read exactly −20 °C and the screen said OK. Why, and how can Room 5 fix it?

  1. The condition is temp < −20. Is −20 less than −20? No, they are equal. So the program runs the else part and shows OK.
  2. The rule includes −20 itself, so the condition needs to be true at −20 too.
  3. Fix 1: use "less than or equal to": if temp ≤ −20.
  4. Fix 2: the board reads whole degrees, so if temp < −19 also works: −20 is less than −19.
  5. Test again at the edges: −21 → WARM WATER, −20 → WARM WATER, −19 → OK.

The bug was at the edge value. Changing the condition to temp ≤ −20 makes the program follow the rule exactly.

Test the edges

When your code compares numbers, test three values: one just past the line, one exactly on it, and one just before it. For Room 5 that was −21, −20 and −19. Bugs love to hide right on the edge.

Hands-on activity

Fridge door alarm (or plan it on paper)

  1. Goal: the board beeps if the fridge door is left open. A fridge light turns on when the door opens, so a light sensor can tell.
  2. Write the code: forever: if light level > 30 then play a beep and show "CLOSE IT", else clear the screen.
  3. If you have a coding board, test it in a dark cupboard and then shine a flashlight on it.
  4. No board? Draw the code blocks on paper and trace it with light levels 10, 30 and 80.
  5. Find the edge: what happens at exactly 30? Is that what you want?
Joke

Why did the thermometer get such good marks at school?

It had lots of degrees!

Let's practise

1

In the outdoor-day helper, what is the input?

2

What is the output?

3

Which is colder, −18 °C or −23 °C?

4

Version 1 reads −24 °C. What does it show?

5

Version 1 reads −15 °C. What does it show?

6

Why does the code use a forever loop?

More practice

1

Is the condition "−20 < −20" true or false?

2

Is the condition "−20 ≤ −20" true or false?

3

What does the else part do?

4

In the fridge alarm, which sensor is used and why?

5

Give three edge values to test the fridge alarm with "light level > 30".

Check your understanding

Quiz

1.

In a sensor program, the sensor provides the...

2.

The code says if temp < −20 show WARM WATER else show OK. The sensor reads −20 °C. What shows?

3.

Which value is less than −20?

4.

Why do programmers test edge values?

For parents and teachers

Students write programs that read a sensor, use a forever loop and choose with if-else. They trace programs with different inputs and fix an edge-value bug. The example uses negative temperatures, which Yukon students meet every winter.

How to help at home:

  • Read the outdoor thermometer together and ask, "Colder or warmer than −20? So which number is smaller?"
  • Trace a program on paper with three test values.
  • If you have a coding board or a free block-coding site, try the fridge alarm.

Common mistakes:

  • Thinking −25 is greater than −20 because 25 is bigger than 20.
  • Mixing up < and ≤.
  • Forgetting the forever loop, so the program checks only once.

The −20 °C rule is made up for this lesson.