🔀 Conditional Statements (if / else)
🎯 Lesson Objective
In this lesson, students will understand:
• What conditional statements are
• Why decision-making is important in programming
• How the if statement works
• How the if-else statement works
• How robots make decisions using conditional statements
This lesson introduces decision-making in programming, which allows robots to respond to different situations.
1️⃣ Why Decision Making is Important in Robotics
Robots often need to make decisions based on sensor inputs.
For example:
• If an obstacle is detected → the robot should turn.
• If light is detected → the robot should move toward it.
• If fire is detected → the robot should activate a fan.
These types of decisions are made using conditional statements in programming.
Conditional statements allow the program to check conditions and perform actions based on those conditions.
2️⃣ What is an if Statement?
The if statement is used to check whether a condition is true.
If the condition is true, the program executes a specific block of code.
If the condition is false, the program simply skips that block.
Basic Syntax
if (condition) {
// code to execute if condition is true
}
The program evaluates the condition inside the parentheses.
If the condition is true, the code inside the braces runs.
3️⃣ Example of an if Statement
Suppose a robot reads a sensor value that detects an obstacle.
Example code:
if (distance < 20) {
digitalWrite(motorPin, LOW);
}
Explanation:
• The program checks if the distance is less than 20 cm.
• If the condition is true, the motor stops.
• If the condition is false, the program continues normally.
This allows the robot to react to nearby obstacles.
4️⃣ The if-else Statement
Sometimes we want the program to perform one action if a condition is true and another action if it is false.
This can be done using the if-else statement.
Basic Syntax
if (condition) {
// code if condition is true
}
else {
// code if condition is false
}
The program checks the condition and executes one of the two blocks.
5️⃣ Example of an if-else Statement
Example:
if (lightDetected == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
Explanation:
• If light is detected → LED turns ON.
• If light is not detected → LED turns OFF.
This allows the robot to react differently depending on sensor input.
6️⃣ Multiple Conditions (else if)
Sometimes we need to check multiple conditions.
For this, we can use else if statements.
Example
if (distance < 10) {
stopRobot();
}
else if (distance < 30) {
slowDown();
}
else {
moveForward();
}
Explanation:
• If the obstacle is very close → stop.
• If the obstacle is at medium distance → slow down.
• If there is no obstacle nearby → move forward.
This allows robots to make more complex decisions.
7️⃣ Conditional Statements in Robotics
Conditional statements are used in almost every robotics program.
Examples include:
🚧 Obstacle detection robots
🛤 Line follower robots
🔥 Fire detection robots
💡 Light-sensitive robots
The robot constantly checks sensor values and uses if statements to determine what action to perform.
This decision-making process allows robots to behave intelligently.
8️⃣ Real Example in Robotics
Let’s look at a simple robot behavior.
1️⃣ The ultrasonic sensor measures the distance.
2️⃣ The program checks the distance value.
3️⃣ If the distance is less than 20 cm → the robot turns.
4️⃣ Otherwise → the robot continues moving forward.
This decision is made using a conditional statement.
🚀 What Happens Next
Now that you understand how programs make decisions using conditional statements, the next step is to learn how programs perform complex logical checks using logical operators.