Home · Academy · Robotics & Coding · Algorithms · Conditions: If, Else and Making Decisions

Conditions: If, Else and Making Decisions

Conditions let a program or robot check the current state and run different commands for different results.

LESSON COMPASS

What will you use this page for?

Core idea

Conditions let a program or robot check its current situation and run different commands depending on the result.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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:…

Next connection

Loops: Making Repeated Tasks Easier

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteAlgorithms and sequence
ContentStandard lesson · 1,445 words
Last updated

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:

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:

Mini activity: smart sports bag check

Rules:

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.

  1. Plan: Record the starting state, expected result and the concepts you will use.
  2. Build the smallest version: Make only the essential behaviour work before adding decoration or extra features.
  3. Prepare three tests: Choose a normal case, a boundary case and an invalid or unexpected case.
  4. Record the result: Put the expected and actual results side by side and name a likely cause when they differ.
  5. Change one thing: Revise one decision and repeat the test instead of changing several parts at once.

Success criteria

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

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

Check questions

  1. What is a condition used for?
  2. What is the difference between > and >=?
  3. What is the difference between the AND and OR connectors?
  4. Why should the high-score condition be checked first?
  5. Write pseudocode for a robot that stops if the distance is 10 centimetres or less.

Answers and explanations

  1. 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.
  2. > is true only when the left value is strictly greater than the right one. >= is also true when the two values are equal. So 10 > 10 is false, but 10 >= 10 is true.
  3. AND needs both conditions to be true at the same time. OR needs at least one of the conditions to be true.
  4. 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.
  5. 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

Start QuizBack to Algorithms
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.