Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies · Design and Make: Grade 3 Designers for Others

Coding with Events and If-Then

BC ADST 3: explore technologiesAbout 40 minutes
Learning goals
  • I can use an event to start code, like "when a key is pressed".
  • I can use an if-then to make a program decide.
  • I can trace a short program and say what it will do.

Words to know

event
Something that happens, like a click or a key press, that starts code.
condition
A question with a yes or no answer, like "Is it touching the rock?".
if-then
Code that does something only if a condition is true.
trace
To go through code one step at a time to see what it does.
sprite
A character or object in a coding program.

Code that listens and decides 🖥️

You may have used loops to repeat code. Now you'll learn two new ideas.

1. Events. An event is something that happens, like a click, a tap or a key press. Code can wait for an event and then start. When the space key is pressed, jump.

2. If-then. A program can ask a condition: a question with a yes or no answer. If the answer is yes, then it does something. If it is raining, then take an umbrella.

when right arrow pressed
move 10 steps
if touching rock then
say "Bump!"
move -10 steps
if touching river mouth then
say "Home at last!"

A salmon game. Each press of the right arrow is an event that runs this code.

right arrowpressedmove 10touchingrock?yessay"Bump!"nowait for next press
How the salmon program decides. The event starts the code. The if-then checks a condition.
Worked example

Tracing the salmon game

The salmon starts at 0 steps. The river mouth is at 100 steps. There are no rocks in this level. How many times must you press the right arrow to get the salmon home?

  1. Each press is an event. It runs the code once.
  2. Each time, the salmon moves 10 steps.
  3. Count by 10s: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100. That's 10 presses.
  4. Check the if-then: At 100 steps, the condition "touching river mouth?" is yes, so the salmon says "Home at last!"

10 presses (10 × 10 = 100). On the 10th press, the if-then is true and the salmon says "Home at last!" 🐟

Read it like a sentence

Read if-then code out loud: "If touching rock, then say Bump." If the sentence makes sense, your code probably does too.

Hands-on activity

Unplugged if-then game

Play with family or friends. One person is the "computer caller".

  1. The caller says if-then rules: "If you are wearing blue, then stand up." "If you have a pet, then clap twice."
  2. Everyone checks the condition for themselves and acts only if it is true.
  3. Add an event: "When I ring the bell, everyone sit down."
  4. Take turns being the caller. Try a rule with a condition that is true for nobody!

If you have a device, try events and if-then blocks in a free block-coding app like ScratchJr or Scratch, with a grown-up.

Odd fact

In 1947, computer scientists working with Grace Hopper found a real moth stuck inside a giant computer. They taped it into their notebook as the "first actual case of bug being found".

Let's practise

1

What is an event in coding?

2

Name 2 events.

3

What is a condition?

4

Finish this if-then: "If it is dark, then..."

5

The salmon moves 10 steps each press. Where is it after 4 presses?

6

In the salmon game, when does the salmon say "Bump!"?

More practice

1

A sprite moves 5 steps each time you click. How many clicks to move 30 steps?

2

Is "Is the cat touching the ball?" a condition? Why?

3

What does it mean to trace code?

4

The rule is "If you have a dog, then hop." You have a cat. What do you do?

Check your understanding

Quiz

1.

Which is an event?

2.

"If touching wall, then turn around." What is the condition?

3.

A sprite moves 10 steps each press. It starts at 0. Where is it after 7 presses?

4.

When does an if-then run its "then" part?

For parents and teachers

How to help at home: Use if-then talk in daily life: "If it is below zero, then wear a toque." Play the unplugged if-then game. ScratchJr (tablets) and Scratch (computers) are free and use event and if-then blocks; explore them together and set a time limit.

Common mistakes: Thinking the "then" part always runs, forgetting that an event must happen before the code starts, and mixing up the condition with the action. Tracing code aloud, one step at a time, helps a lot. Counting presses by 10s or 5s links to Grade 3 multiplication.