📘 Lesson P5 – Conditional Statements (Decision Making)
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand decision-making in programming
✅ Use if statements
✅ Use if-else statements
✅ Use else if statements
✅ Create multiple decision conditions
✅ Build smart automation logic
✅ Use conditions in real Arduino projects
1. Introduction
Until now, Arduino programs have simply executed instructions one after another.
But real-world systems need to make decisions.
For example:
Smart Fan
If temperature is high
↓
Turn ON fan
Otherwise
↓
Keep fan OFF
Automatic Street Light
If it is dark
↓
Turn ON light
Otherwise
↓
Turn OFF light
Obstacle Avoiding Robot
If obstacle detected
↓
Stop
Otherwise
↓
Move Forward
To make such decisions, we use:
Conditional Statements
2. What is a Conditional Statement?
A conditional statement allows Arduino to execute different instructions based on a condition.
Think of it as asking a question.
Example:
Is temperature greater than 40°C?
If YES:
Turn ON fan
If NO:
Do nothing
3. Real-Life Example
Imagine traffic lights.
Condition:
Is the signal RED?
YES
↓
Stop Vehicle
NO
↓
Move Forward
Traffic systems continuously make decisions.
Arduino works in a similar way.
4. The if Statement
The simplest decision-making statement.
Syntax
if(condition)
{
// Code
}
How It Works
Arduino checks the condition.
If condition is TRUE:
Execute code inside braces.
If condition is FALSE:
Skip the code.
Example
int temperature = 45;
if(temperature > 40)
{
Serial.println("Fan ON");
}
Output:
Fan ON
Because:
45 > 40
is TRUE.
Example with FALSE Condition
int temperature = 30;
if(temperature > 40)
{
Serial.println("Fan ON");
}
Output:
Nothing
Because:
30 > 40
is FALSE.
5. Flowchart of if Statement
Start
↓
Check Condition
↓
TRUE? ---- YES ----> Execute Code
↓
NO
↓
Skip Code
↓
Continue Program
6. The if-else Statement
Sometimes we want two possible actions.
Example:
If it is raining
↓
Carry umbrella
Otherwise
↓
Do not carry umbrella
For this we use:
if-else
Syntax
if(condition)
{
// True Code
}
else
{
// False Code
}
Example
int temperature = 45;
if(temperature > 40)
{
Serial.println("Fan ON");
}
else
{
Serial.println("Fan OFF");
}
Output:
Fan ON
Example
int temperature = 25;
if(temperature > 40)
{
Serial.println("Fan ON");
}
else
{
Serial.println("Fan OFF");
}
Output:
Fan OFF
7. The else if Statement
Sometimes we have multiple conditions.
Example:
Temperature:
- Above 40 → High
- Above 30 → Medium
- Below 30 → Low
This requires:
else if
Syntax
if(condition1)
{
}
else if(condition2)
{
}
else
{
}
Example
int temperature = 35;
if(temperature > 40)
{
Serial.println("High");
}
else if(temperature > 30)
{
Serial.println("Medium");
}
else
{
Serial.println("Low");
}
Output:
Medium
How Arduino Checks Conditions
Step 1:
Check first condition
↓
If TRUE
↓
Execute and stop checking
If FALSE
↓
Check next condition
↓
Continue until a match is found
8. Nested if Statements
An if statement can exist inside another if statement.
Example:
if(cardDetected)
{
if(passwordCorrect)
{
Serial.println("Access Granted");
}
}
Used in:
- Security Systems
- Smart Locks
- Login Systems
9. Using Logical Operators with Conditions
Conditions become powerful when combined with:
AND (&&)
OR (||)
NOT (!)
Example: AND Operator
if(age > 18 && age < 60)
{
Serial.println("Eligible");
}
Both conditions must be TRUE.
Example: OR Operator
if(score > 90 || attendance > 95)
{
Serial.println("Reward");
}
Only one condition needs to be TRUE.
Example: NOT Operator
if(!buttonPressed)
{
Serial.println("Button Not Pressed");
}
Reverses the condition.
10. Real Arduino Project Examples
Automatic Street Light
if(lightValue < 300)
{
digitalWrite(LED,HIGH);
}
else
{
digitalWrite(LED,LOW);
}
When dark:
LED turns ON.
Water Tank Monitoring
if(level > 90)
{
pumpOFF();
}
Tank full.
Gas Leakage Detection
if(gasValue > 500)
{
buzzerON();
}
Gas detected.
Obstacle Avoiding Robot
if(distance < 10)
{
stopRobot();
}
else
{
moveForward();
}
11. Multiple Conditions Example
Student Grade System
int marks = 75;
if(marks >= 90)
{
Serial.println("A");
}
else if(marks >= 80)
{
Serial.println("B");
}
else if(marks >= 70)
{
Serial.println("C");
}
else
{
Serial.println("Fail");
}
Output:
C
12. Common Beginner Mistakes
Mistake 1
Using:
=
instead of:
==
Wrong:
if(temp = 30)
Correct:
if(temp == 30)
Mistake 2
Missing braces.
Wrong:
if(temp > 30)
Serial.println("Hot");
Use braces for clarity.
Mistake 3
Incorrect condition order.
Example:
if(marks > 50)
before
if(marks > 90)
can create logic errors.
Mistake 4
Using too many nested if statements.
Makes code difficult to read.
13. Best Practices
✅ Use meaningful conditions
✅ Keep logic simple
✅ Use indentation properly
✅ Use comments when necessary
✅ Test all conditions
14. Why Conditional Statements are Important
Almost every Arduino project uses decision making.
Examples:
- Smart Door Lock
- Weather Station
- Water Level Controller
- Fire Alarm
- Line Follower Robot
- Obstacle Avoiding Robot
- Home Automation
Without conditional statements, automation would not be possible.
📊 Summary
In this lesson, we learned:
✅ if Statement
✅ if-else Statement
✅ else if Statement
✅ Nested if Statements
✅ Logical Operators with Conditions
✅ Real-world decision-making systems
Conditional statements allow Arduino to make intelligent decisions based on sensor values, user inputs, and program logic.
📖 Key Terms
Condition
An expression that evaluates to TRUE or FALSE.
if
Executes code when condition is TRUE.
else
Executes code when condition is FALSE.
else if
Checks additional conditions.
Nested if
An if statement inside another if statement.
Decision Making
Choosing actions based on conditions.
🎯 Quiz
1. Which statement is used for decision making?
A. loop()
B. if ✅
C. setup()
D. pinMode()
2. What happens if an if condition is FALSE?
A. Program Stops
B. Error Occurs
C. Code Inside if is Skipped ✅
D. Arduino Restarts
3. Which statement handles multiple conditions?
A. while
B. else if ✅
C. setup
D. Serial
4. Which operator means AND?
A. ||
B. !
C. && ✅
D. ==
5. Conditional statements are mainly used for:
A. Decision Making ✅
B. Memory Storage
C. Power Supply
D. Uploading Code
🏠 Assignment
Task 1
Write a program that checks whether a number is positive or negative.
Task 2
Create a temperature monitoring logic that turns ON a fan above 35°C.
Task 3
Create a grade calculator using if-else if.
Task 4
Write three examples using AND (&&) and OR (||) operators.
Task 5
Design the logic for an automatic street light using conditional statements.