One-sentence summary
Conditions let a program or robot check its current situation and run different commands depending on the result.
Why it matters
If a robot does exactly the same thing in every situation, it cannot respond to changes around it. It would keep moving even with an obstacle in front of it, switch on a lamp even when the room is already bright, or carry on working even as its battery runs low. Conditions give a system the ability to make decisions.
The basic shape of a condition
If the condition is true
do one action
Otherwise
do a different action
Example:
If it is raining
take the umbrella
Otherwise
do not take the umbrella
Here the question "is it raining?" has only two possible answers: yes or no.
Comparisons
Comparisons used often inside conditions:
- Equal to?
== - Not equal to?
!= - Greater than?
> - Less than?
< - Greater than or equal to?
>= - Less than or equal to?
<=
Python example:
temperature = 29
if temperature >= 30:
print("It is very hot")
else:
print("The temperature is below 30 degrees")
Robotics example: detecting an obstacle
Measure the distance
If the distance is less than 15 centimetres
stop the motors
turn right
Otherwise
move forward
The robot makes a fresh decision with every measurement. Instead of following one fixed set of movements, it reacts to its surroundings.
More than two outcomes
Sometimes two outcomes are not enough.
score = 78
if score >= 90:
print("Very good")
elif score >= 70:
print("Good")
else:
print("Review the topic again")
Here the program checks the conditions from top to bottom, and the first true branch is the one that runs.
Logical connectors
AND
Both conditions must be true.
If the helmet is on AND the brakes are working
move on to the next step of the ride check
OR
At least one of the conditions must be true.
If the right sensor OR the left sensor sees an obstacle
slow down
NOT
Checks the opposite of a situation.
If the door is NOT closed
go inside
Nested conditions
A decision can contain another decision inside it:
If the battery level is high enough
If the way ahead is clear
move forward
Otherwise
stop and change direction
Otherwise
return to the charging station
Nested conditions are useful, but if there are too many of them they can make the code hard to read. When that happens, it helps to break the problem into smaller functions.
Testing boundary values
One of the most common mistakes with conditions is thinking about the threshold value incorrectly.
If the distance is less than 10, stop
What happens when the distance is exactly 10? The system will not stop. But if 10 should be included for safety, the condition needs to be written like this:
If the distance is less than or equal to 10, stop
In testing, you should try the value just below the threshold, the threshold itself, and the value just above it:
- 9
- 10
- 11
Mini activity: smart sports bag check
Rules:
- If it is basketball practice, the ball and water should be checked.
- If it is swimming practice, the cap, goggles and towel should be checked.
- If the water bottle is empty, it should be filled.
- If anything is missing, leaving should not be allowed.
Pseudocode:
Get the type of practice
If the type is basketball
check the ball
check the water
Otherwise if the type is swimming
check the cap
check the goggles
check the towel
If the water bottle is empty
fill the water bottle
If anything is missing
show the warning "Complete what is missing"
Otherwise
show the message "You are ready"
Practice lab: Conditions: If, Else and Making Decisions
The best way to retain Conditions: If, Else and Making Decisions is to turn the idea into a small, measurable task. In this activity you will connect The basic shape of a condition with Robotics example: detecting an obstacle and produce a clear algorithm, pseudocode and a test table. The aim is not only to make the result work. You should also be able to explain why you made each decision, what you tested and which observation would make you revise the design.
Challenge scenario
Work with this scenario: a decision flow that counts repetitions in a sports drill. Because the main goal of the lesson is to “Conditions let a program or robot check the current state and run different commands for different results”, begin by defining the problem in one sentence. Then write the input, the process and the output separately. Mark anything you do not know as an assumption rather than presenting it as a fact.
- Plan: Record the starting state, expected result and the concepts you will use.
- Build the smallest version: Make only the essential behaviour work before adding decoration or extra features.
- Prepare three tests: Choose a normal case, a boundary case and an invalid or unexpected case.
- Record the result: Put the expected and actual results side by side and name a likely cause when they differ.
- Change one thing: Revise one decision and repeat the test instead of changing several parts at once.
Success criteria
- Are the steps clear and unambiguous?
- Are the start and finish conditions defined?
- Were normal, boundary and invalid cases tested?
- Is there an unnecessary step or repetition?
- Can another person follow the algorithm and obtain the same result?
After completing “Conditions: If, Else and Making Decisions”, explain the work to a classmate using only the section headings. If the classmate can follow the decisions in the scenario of a decision flow that counts repetitions in a sports drill, the explanation is clear enough. Fix an unclear point by dividing the relationship between The basic shape of a condition and Robotics example: detecting an obstacle into smaller steps rather than adding jargon.
Common mistakes
- Mixing up the
=and==signs. - Not thinking about whether the threshold value is included or not.
- Not defining the "otherwise" case.
- Writing conditions that contradict each other.
- Writing the more general condition first, so the more specific condition never runs.
Example of the wrong order:
if score >= 70:
print("Passed")
elif score >= 90:
print("Excellent")
Someone who scores 95 stops at the first condition, so the "Excellent" result never runs. The more specific, higher threshold should be written first.
Safety note
This lesson is at a beginner level. When you move these ideas onto a real robot, work with an adult whenever motors, batteries or moving parts are involved. Motors can start suddenly, so keep your hands, hair and loose cables away from the wheels and test at a low speed first. For any stop that matters for safety, choose the threshold carefully and include the boundary value with <=, so the robot still stops at the exact limit rather than just past it.
Lesson summary
- Conditions give a system the ability to make decisions.
- "If / otherwise" is the most basic decision structure.
- Comparisons and logical connectors build more detailed decisions.
- Threshold values should be checked with boundary tests.
- The order of the conditions can change the result.
Check questions
- What is a condition used for?
- What is the difference between
>and>=? - What is the difference between the AND and OR connectors?
- Why should the high-score condition be checked first?
- Write pseudocode for a robot that stops if the distance is 10 centimetres or less.
Answers and explanations
- A condition lets a program or robot check its current situation and choose different commands for different results, instead of always doing the same thing.
>is true only when the left value is strictly greater than the right one.>=is also true when the two values are equal. So10 > 10is false, but10 >= 10is true.- AND needs both conditions to be true at the same time. OR needs at least one of the conditions to be true.
- Conditions are checked from top to bottom, and the first true branch runs. If the lower threshold (
>= 70) is written first, a score of 95 stops there and the higher branch (>= 90) never runs. The more specific, higher threshold must come first. - One possible answer:
Measure the distance
If the distance is less than or equal to 10
stop the motors
Otherwise
keep moving
Source and verification note
For “Conditions: If, Else and Making Decisions”, verification focuses on whether the relationship between The basic shape of a condition and Robotics example: detecting an obstacle remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.
Next lesson
Loops: Making Repeated Tasks Easier