← Back to course
2/6
Week 05 Β· Rules or Learning?

Tuesday

If-then rules
// Two ways to build a helper
⏱ about 15 min

Tuesday: If-Then Rules

Raven writes a list of rules on the whiteboard. Each one starts with if.

"If it has a handle, then tool," Rocket reads. "That one is easy!"

Raven taps the list. "What do you notice? The order of the rules matters."

Nova hovers by the board. "Here is a hint," she says. "Check rule 1 first. Stop at the first match."

How if-then rules work

An if-then rule has a test and an action.

If the test is true, then the computer does the action.

A list of rules is checked from the top. The first rule that matches wins.

The computer follows the rules exactly. It never changes them by itself.

RuleIf...Then...
1it has a handlelabel it tool
2it has leaveslabel it plant
3none of the rules above matchlabel it rock
RUN THE RULES
  • Read the question.
  • Tap your answer.
A photo shows a hammer with a handle. Which rule matches first?
A photo shows a fern with leaves. Which label?
A photo shows a gray lump. Which label?
A photo shows a metal ruler with no handle. Which label do these rules pick?
HOW A COMPUTER CHECKS A RULE LIST
  • ?Check if the test is true.
  • ?If it is false, go to the next rule.
  • ?If it is true, do the action and stop.
  • ?Read rule 1.
WHY THIS EXERCISEA computer checks rules in order and follows them exactly.

When rules work well

Rules work well when the job is clear.

Rules are easy to read. You can see exactly why the computer chose.

But when there are many tricky cases, the list of rules gets very long.

StatementTrue or false?
An if-then rule has a test and an action.?
The computer checks the rules from the bottom up.?
You can read a rule to see why the computer chose.?
The computer changes its rules all by itself.?
WHY THIS EXERCISERules are clear and exact, and people write and change them.
If a photo has a handle, the rules pick the label ____.
How many rules are in Raven's list?
The rule that says what happens is the ____ part.
Rules are checked from the top. Which number rule is checked first?
WHY THIS EXERCISEOrder matters. The first rule that matches wins.
Try it
Write three if-then rules for getting ready in the morning.
Example: if it is raining, then wear boots.
Ask a grown-up to give you a pretend day. Follow your rules.

You wrote rules like a programmer! Tomorrow the lab tests rules and examples side by side.

← Monday