Unit 1 · Big Idea 3
Computers Make Decisions Using Rules
Student Lab · The Freight Sorter
Student PIN:
Overview
So far, your robot has done exactly what you told it, in exactly the order you wrote it. Today that changes. You’ll write a program that makes a choice — it looks at a value and decides what to do based on a rule. The robot will sort freight: one action for one kind, a different action for another.
Core Insight
A decision is a rule the computer follows: “IF this is true, do one thing; OTHERWISE, do something else.”
The robot doesn’t “know” anything. It checks a value against a rule you wrote — and that is what makes it look intelligent.
By the end of this activity you will be able to:
- Explain what a is and how an
ifuses one to make a decision. - Use
if/elseto make a robot take different actions for different values. - Predict what a program will do by reading its rule before you run it.
- Connect rule-based decisions to how intelligent systems classify and respond to the world.
New This Time: Making a Decision
You already know int, , and the commands motor(), msleep(), and ao(). Today you add one new tool: the if statement.
An if statement checks whether something is true. If it is, the computer runs the code inside the curly braces. If it is not, it can run a different after else instead.
int freight_type = 1; // a number YOU set
if (freight_type == 1) // IF freight_type is 1...
{
drive_forward(); // ...do this
}
else // OTHERWISE...
{
turn_right(); // ...do this instead
}Change freight_type to a different number, and the robot makes a different choice — without you rewriting the rule.
Watch the symbol carefully. There are two equals signs in a rule, not one:
int freight_type = 1; // ONE equals = SETS the value to 1
if (freight_type == 1) // TWO equals == ASKS if it is equal to 1
A single = sets a value. A double == asks a question. Using one when you mean the other is one of the most common bugs in all of programming — so check it every time.
Phase 1 — Activate: The Bouncer at the Door
Imagine you are a bouncer at the door of an event. You have exactly one rule: if a person’s ticket says “VIP,” send them left; otherwise, send them right. You don’t know these people. You don’t judge them. You just check the ticket against the rule and act.
Think it through
You see 5 people in a row. Their tickets say: VIP, General, VIP, General, General. Which way does each person go?
Did you have to “think” about each person, or did you just apply the rule?
Send each person left or right by your rule:
| Person | Ticket says | Left or Right? |
|---|---|---|
| 1 | VIP | |
| 2 | General | |
| 3 | VIP | |
| 4 | General | |
| 5 | General |
A bouncer follows a rule without deciding who “deserves” what. How is that like the way a computer makes a decision?
Phase 2 — Concept: Conditions, Rules, and Classification
A Condition Is a True/False Question
Every decision a computer makes starts with a condition — a question that is either true or false. freight_type == 1 is a condition. Right now it is either true (the value really is 1) or false (it is anything else). There is no “maybe.” This is called thinking: every condition is true or false, nothing in between.
Classification
When a computer sorts things into groups using rules, that is classification. A mail machine reads a ZIP code and sends each letter to the right bin. Your program reads a freight value and sends the robot in the right direction. Same idea: a rule turns information into an action.
Pattern Recognition
Rules let a system respond to a pattern instead of a single fixed case. Your one rule — “if type 1, go straight; else turn” — handles every freight value you could ever set, not just one. Write the rule once, and it works for the whole pattern of cases.
Example
“If the light is red, stop; otherwise, go.” That one rule covers every red light and every non-red light in the world. You don’t write a new rule for each intersection — the pattern is handled by a single condition.
In your own words: what is the difference between a program that follows a fixed list of steps and one that makes a decision?
Phase 3 — Plan
The Sorting Task
Your Goal
Your robot is a freight sorter. A freight value is set at the top of the program. Your robot must take one action if the freight is type 1, and a different action if it is anything else.
You choose what “type 1” and “anything else” mean for the robot — for example, drive straight to one bin, or turn toward another.
Step 1 — Write Your Rule in Plain English
Before any code, write your sorting rule as a sentence in the form “IF … THEN … OTHERWISE …”.
Step 2 — Predict the Two Outcomes
Fill in what the robot should do for each freight value.
If freight_type is… | …the robot should |
|---|---|
| 1 | |
| anything else (2, 3, …) |
Step 3 — Trace the Rule by Hand
Before you run anything, predict the robot’s action for each value. This is called tracing — following the rule the way the computer will.
freight_type set to | Condition true or false? | What the robot does |
|---|---|---|
| 1 | ||
| 2 | ||
| 5 |
Phase 4 — Build & Run
Starting Code Template
Type this program into your robot . The rule lives inside main(). Change freight_type at the top to test both paths. Fill in the actions to match your Phase 3 plan.
// Unit 1, Big Idea 3: Freight Sorter
// Name: _______________________ Date: ___________
#include <kipr/wombat.h>
int DRIVE_SPEED = 50;
// A number YOU set. Think of it as the freight you're sorting.
// Change this value and the robot makes a different decision.
int freight_type = 1; // try 1, then try 2
void drive_forward()
{
motor(0, DRIVE_SPEED);
motor(3, DRIVE_SPEED);
msleep(1000);
ao();
}
void turn_right()
{
motor(0, DRIVE_SPEED);
motor(3, -DRIVE_SPEED);
msleep(600);
ao();
}
int main()
{
if (freight_type == 1) // IF the freight is type 1...
{
drive_forward(); // ...send it straight ahead
}
else // OTHERWISE (it's not type 1)...
{
turn_right(); // ...send it to the right instead
}
return 0;
}Run It Both Ways — Results
Run the program with freight_type = 1, then change it to 2 and run again. Record what the robot actually did.
freight_type value | What you predicted | What actually happened |
|---|---|---|
| 1 | ||
| 2 |
- You used
==(two equals) inside theif, not a single= - Every
{has a matching} - You tested BOTH values, not just one
- The robot did something different for each value
Phase 5 — Debug
Decisions create a brand-new kind of bug: the robot does the wrong action, even though it runs without an error. That means the rule ran fine — but it was the wrong rule, or the value was not what you thought.
The most common decision bugs
Single = instead of ==: writing if (freight_type = 1) sets the value instead of asking the question. Always use == in a rule.
Wrong value: the robot turned when you expected straight? Check what freight_type is actually set to at the top.
Mismatched braces: a missing } makes the program refuse to build. Count them.
Log
| Try | What went wrong | Why (your best guess) | How you fixed it |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
Describe one decision bug you hit. Did the robot do the wrong thing, or refuse to run? How did you find the cause?
Phase 6 — Connect: The AI Literacy Bridge
Big Idea 3 --- AI Literacy Thread
Intelligent systems identify patterns and use rules to make decisions.
Your robot did not “understand” the freight. It checked a value against a rule you wrote and acted. Almost every intelligent system works this way underneath: an email app checks features of a message against rules and decides “spam or not spam.” A photo app checks patterns and decides “face or not a face.” The system is not judging — it is classifying with rules. The intelligence is in the rules, and a human wrote them.
Read each scenario. Think it through, then write your answer.
A self-checkout machine decides whether the item you scanned matches its weight. What is the rule it is checking? What happens when the rule is wrong?
Your freight sorter only knows two outcomes: type 1, or everything else. A real sorting system might have ten kinds of freight. What is one problem with a rule that lumps everything that isn’t type 1 into a single “else”?
Who is responsible when an automated system classifies something wrong — sends the wrong freight, flags the wrong email? Connect this to the fact that a person wrote the rule.
Phase 7 — Individual Reflection
Complete this section on your own.
1. What is a condition? Write a definition in your own words.
2. Explain the difference between = and ==. Why does it matter inside an if statement?
3. Today your robot took different actions for different values without you rewriting the rule. Why is making a decision more powerful than a fixed list of steps?
4. Complete this in 2–3 sentences: “Intelligent systems identify patterns and use rules to make decisions. This means that when an AI system classifies something wrong…”
Extension Challenges
Finished early? Try one or more of these.
Extension A — A Third Choice
- Right now your rule has two outcomes. Add a middle case using
else if: type 1 does one thing, type 2 does another, everything else does a third. - Trace your new rule by hand for
freight_type= 1, 2, and 9 before you run it.
Extension B — Flip the Rule
- Change your condition so the robot does the OPPOSITE — drives straight for everything except type 1.
- What did you change? Did you change the condition, the actions, or both?
Extension C — Greater Than
==is not the only test. Tryif (freight_type > 3)— “is the value greater than 3?”- Predict, then test: what does the robot do for values 1, 3, and 7?
Extension D — Two Rules in a Row
- What happens if you write two separate
ifstatements one after another, each checkingfreight_type? - Could the robot ever do two actions in one run? When would that be useful, and when would it be a bug?
Extension E — Random Freight
- Real freight wouldn’t always be the same type every run. Use
rand() % 10to generate a randomfreight_typeeach time your program runs (you’ll need#include <stdlib.h>,#include <time.h>and a call tosrand(time(NULL))once at the top ofmain()). - Run it several times. Does your sorting rule still make the correct choice across many random values?
When you are finished, press the button to turn in your work and save a copy.
KIPR · Botball Explorer · Unit 1 Big Idea 3 — Student Lab