← All articles

16 November 2026

If-Else: How Does a Robot Make Decisions?

Illustration of a robot making a decision with an if-else statement

An obstacle-avoiding robot turns away from a wall. A lamp switches on when it gets dark. A fan starts when the room gets warm. From the outside, these actions look like the robot is "thinking" and deciding. But inside, a robot does not think like a person — it follows one simple rule every programmer knows: if — then — otherwise.

That rule is called if-else, the conditional statement. And the good news is your child already uses it every day without knowing: if it rains, take an umbrella; if you are hungry, eat; if homework is done, go play. Understanding if-else is the first step from being a technology user to being someone who gives technology orders.

What is if-else? One question, two paths

If-else is how code asks a question with a true or false answer, then picks what to do based on that answer. Its structure is always the same:

Take an obstacle-avoiding robot: if the ultrasonic sensor sees an obstacle closer than 20cm, turn left; otherwise, go straight. The robot does not need to understand what the obstacle is. It only measures a number, compares it with 20, and picks one of two paths.

In everyday life, your child decides the same way: if it rains, take an umbrella; if not, leave it at home. If the traffic light is red, stop; if it is green, go. Every time your child picks one of two paths based on a condition, they are using if-else in their head.

Sensors ask the question, if-else answers it

On its own, if-else does nothing — it needs data. In a robot, data comes from sensors: an ultrasonic sensor measures distance, a light sensor measures brightness, a temperature sensor measures heat. Each sensor returns a number, and if-else compares that number with a threshold the programmer chose.

Look at a lamp that switches on when it gets dark: the light sensor reads a brightness of 15. The code asks: is the brightness less than 30? The answer is yes, so the lamp turns on. Later, when it gets bright again, the reading rises to 80. The same question — is the brightness less than 30? — now gets the answer no, so the lamp turns off.

The number 30 is not built into nature. The programmer picks it after testing: 30 is the point where human eyes start to feel it is dark. Choosing a threshold is an engineering skill — too high and the lamp turns on in broad daylight, too low and it turns on too late when it is already dark. Your child will learn to pick thresholds by trying, measuring, and adjusting — exactly how real engineers work.

Writing the first statement together

A simple if-else snippet for an obstacle-avoiding robot looks like this:

if (distance < 20) {
  turn_left();
} else {
  go_straight();
}

Read this code out loud with your child: "If the distance is less than 20 centimeters, turn left. Otherwise, go straight." There is nothing mysterious here — it is just an everyday sentence rewritten in symbols a machine understands. When your child can read code as words, they already understand code, even if they have never studied programming.

A good tip for parents: do not explain the syntax first. Start with an everyday example, then show your child how that example looks written as code. The order matters: understand the idea first, learn the symbols second. Kids are not scared of code because the logic is hard — they are scared because the symbols look strange. Once the logic feels familiar, the symbols are a small thing.

When two choices are not enough: else-if

Real life rarely has only two paths. Take a delivery robot in a house: if the way ahead is clear, go straight; if there is an obstacle on the left, turn right; if there are obstacles on both sides, back up. This is where else-if comes in — a chain of questions asked one after another:

if (path_clear) {
  go_straight();
} else if (obstacle_on_left) {
  turn_right();
} else {
  go_back();
}

The machine asks each question in order and stops at the first one with a true answer. So the order of the questions matters a lot: if your child puts a vague question first, the detailed question after it will never be asked. This is a very common mistake — and a great thinking lesson: order your questions from specific to general, from urgent to ordinary.

Several conditions can also join into one question: if it is dark AND someone is in the room, turn on the light. If the temperature is high OR the humidity is high, turn on the fan. The words AND and OR are the next step after your child is comfortable with basic if-else.

Common mistakes — and how to fix them

Kids new to if-else usually make three mistakes, and all three are good lessons:

Every mistake has a fix, and fixing mistakes — debugging — is half of an engineer's job. When your child finds out on their own why the robot turned the wrong way, they learn more than when the code worked the first time.

If-else games that need no computer

You can teach if-else at the dinner table, with no screen at all:

These games teach your child conditional thinking before they ever touch a keyboard — and when they open a code editor for the first time, if-else will already be an old friend.

From one statement to a whole robot

If-else looks small, but a whole robot is hundreds of if-else statements stacked together: sensors ask, code answers, motors act. Delivery robots, obstacle-avoiding robots, lamps that switch themselves on, fans that start by themselves — all of them begin with one true-or-false question.

When your child understands if-else, they will look at every device around them with different eyes: no longer a mysterious black box, but a set of decisions that can be read, understood, and one day — written by themselves.

Start today, no kit required

Your child can try writing their very first if-else statement today on ManuStem — a browser-based robotics platform, no kit required, for kids ages 8+, with 28 lessons including 8 Component Lab lessons and 20 Competition Robot lessons, 4 of them free to try. The built-in AI co-pilot helps your child wire circuits, write code, and debug step by step, while the 3D robot preview shows them their creation before it runs. Start at stem.manusignal.com — in Vietnamese or English, your child's choice.

Read the Vietnamese version:

If-else: robot ra quyết định như thế nào?