Course Content
IoT Engineering Course using ESP32 Wifi Robots

📘Conditional Statements

(if, else, else if – Deep Explanation for IoT Programming)


🎯 Lesson Objective

By the end of this lesson, students will:

  • Understand decision-making in programming

  • Learn how if, else, and else if work

  • Understand logical flow control

  • Build automation logic

  • Create real IoT decision systems

  • Avoid common mistakes


1️⃣ What is a Conditional Statement?

A conditional statement allows a program to make decisions.

In simple words:

If something is true → Do this
Otherwise → Do something else

This is how automation works.


2️⃣ Why Conditional Statements Are Important in IoT

IoT systems are based on conditions.

Examples:

If temperature > 30°C → Turn AC ON
If gas detected → Trigger alert
If water tank full → Stop pump
If motion detected → Send notification

All of this is built using conditional statements.


3️⃣ The if Statement

🔹 Basic Syntax

 
if (condition) {
// code to execute
}
 

If the condition is true → block runs
If false → block is skipped


📌 Example 1 – Simple Temperature Check

 
int temperature = 32;

if (temperature > 30) {
Serial.println("AC ON");
}
 

If temperature is greater than 30 → prints message.


4️⃣ The if – else Statement

Used when there are two possible outcomes.


🔹 Syntax

 
if (condition) {
// if true
} else {
// if false
}
 

📌 Example – AC Control

 
int temperature = 28;

if (temperature > 30) {
Serial.println("AC ON");
} else {
Serial.println("AC OFF");
}
 

Only one block executes.


5️⃣ The else if Statement

Used when there are multiple conditions.


🔹 Syntax

 
if (condition1) {
}
else if (condition2) {
}
else {
}
 

Checks conditions in order.

Stops at first true condition.


📌 Example – Temperature Levels

 
int temperature = 35;

if (temperature > 40) {
Serial.println("Very Hot");
}
else if (temperature > 30) {
Serial.println("Hot");
}
else {
Serial.println("Normal");
}
 

Only one block runs.


6️⃣ Real IoT Example – Gas Detection System

 
int gasValue = 900;

if (gasValue > 1000) {
Serial.println("Danger Level!");
}
else if (gasValue > 700) {
Serial.println("Warning Level!");
}
else {
Serial.println("Safe Level");
}
 

This builds multi-level alert system.


7️⃣ Using Logical Operators Inside Conditions

Conditions can be combined.

Example:

 
if (temperature > 30 && manualMode == false) {
Serial.println("Auto AC ON");
}
 

This means:

Temperature must be high
AND
Manual mode must be OFF

Then activate AC.


8️⃣ Nested Conditions

You can place one condition inside another.


📌 Example

 
if (manualMode == true) {
if (buttonPressed == true) {
Serial.println(“Manual Control Activated”);
}
}
 

Nested conditions allow complex logic.


9️⃣ Flow of Conditional Execution

Conditions are checked in order.

For else if:

If first condition is true → others ignored.

Example:

 
if (temperature > 30) {
}
else if (temperature > 20) {
}
 

If temperature = 35
Only first block executes.


🔟 Real Smart AC Example (Manual + Auto Mode)

 
if (manualMode == true) {
if (buttonState == 1) {
acState = true;
} else {
acState = false;
}
}
else {
if (temperature > setPoint) {
acState = true;
} else {
acState = false;
}
}
 

This is real IoT automation structure.

Manual mode → User control
Auto mode → Temperature-based control


1️⃣1️⃣ Common Beginner Mistakes

❌ Missing parentheses

Wrong:

 
if temperature > 30
 

Correct:

 
if (temperature > 30)
 

❌ Using = instead of ==

Wrong:

 
if (temperature = 30)
 

Correct:

 
if (temperature == 30)
 

❌ Forgetting braces { }

Always use braces for clarity.


1️⃣2️⃣ Best Practices

✔ Keep conditions simple
✔ Use meaningful variable names
✔ Avoid deep nesting when possible
✔ Print debugging messages
✔ Test with Serial Monitor


1️⃣3️⃣ Building Automation Thinking

Before writing code, think:

What conditions exist?

Example:

Smart Pump Logic:

If waterLevel < 30% → Pump ON
If waterLevel > 90% → Pump OFF

Break logic first.
Then code.


📌 Lesson Summary

In this lesson, we learned:

  • What conditional statements are

  • How if works

  • How if-else works

  • How else if works

  • Nested conditions

  • Logical combinations

  • Real IoT examples

Conditional statements are the brain of automation.

Scroll to Top