📘Loops
🎯 Lesson Objective
By the end of this lesson, students will:
-
Understand what a loop is
-
Learn how
forloop works -
Learn how
whileloop works -
Understand infinite loop concept
-
Understand how
loop()function is an infinite loop -
Apply loops in IoT logic
1️⃣ What is a Loop?
A loop is a programming structure that repeats a block of code multiple times.
In simple words:
A loop allows a program to do something again and again.
Repetition is essential in automation systems.
2️⃣ Why Loops Are Important in IoT
IoT systems must:
-
Continuously read sensors
-
Continuously check conditions
-
Continuously send data
-
Continuously monitor state
Without loops:
System would run once and stop.
Microcontrollers must keep working forever.
3️⃣ Types of Loops in Arduino / C++
We will focus on:
1️⃣ for loop
2️⃣ while loop
3️⃣ Infinite loop concept
🔹 4️⃣ The for Loop
Used when you know how many times to repeat something.
🔹 Syntax
for (initialization; condition; update) {
// code block
}
Structure:
-
Initialization → runs once
-
Condition → checked before each iteration
-
Update → runs after each iteration
📌 Example 1 – Count from 1 to 5
Serial.println(i);
}
Output:
1
2
3
4
5
Explanation:
-
i starts at 1
-
Runs while i <= 5
-
After each loop → i increases
📌 Real IoT Example – LED Blink 5 Times
for (int i = 0; i < 5; i++) {
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}
LED blinks exactly 5 times.
🔹 5️⃣ The while Loop
Used when repetition depends on a condition.
🔹 Syntax
while (condition) {
// code block
}
The loop runs as long as condition is true.
📌 Example – Count Until 5
int i = 1;
while (i <= 5) {
Serial.println(i);
i++;
}
Runs until condition becomes false.
📌 Real IoT Example – Wait for WiFi Connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting...");
}
Loop runs until WiFi connects.
This is very common in IoT systems.
6️⃣ Infinite Loop Concept
An infinite loop runs forever.
Example:
}
This loop never stops.
🔹 How Arduino Uses Infinite Loop
Remember:
Arduino has:
void loop() {
}
Internally, it behaves like:
while(true) {
loop();
}
That means:
loop() runs forever.
Microcontrollers are designed for continuous operation.
7️⃣ Difference Between for and while
| Feature | for Loop | while Loop |
|---|---|---|
| Known repetitions | Yes | No |
| Condition-based | Limited | Yes |
| Used for counting | Yes | Sometimes |
| Used for waiting | Rare | Very common |
8️⃣ Using break in Loops
Sometimes we want to stop loop early.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
Serial.println(i);
}
Stops at 5.
Break exits loop immediately.
9️⃣ Using continue in Loops
Continue skips current iteration.
Example:
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
Serial.println(i);
}
Skips printing 2.
🔟 Real IoT Example – Sensor Monitoring
void loop() {
int temperature = analogRead(34);
if (temperature > 30) {
Serial.println("High Temperature");
}
delay(1000);
}
loop() keeps checking sensor continuously.
This is real-time monitoring.
1️⃣1️⃣ Dangerous Infinite Loop
Example:
while (true) {
Serial.println("Hello");
}
This will:
-
Flood Serial Monitor
-
Freeze system
Always use delay or proper condition.
1️⃣2️⃣ Blocking vs Non-Blocking Thinking
Using delay():
delay(5000);
System waits 5 seconds doing nothing.
In advanced IoT systems, this is not ideal.
Later in course, you may learn:
millis() based timing (non-blocking logic).
For now, delay is acceptable.
1️⃣3️⃣ Combining Loops and Conditions
Example:
int gasValue = 0;
while (gasValue < 1000) {
gasValue = analogRead(34);
Serial.println(gasValue);
}
System keeps checking gas until threshold reached.
This builds safety monitoring logic.
1️⃣4️⃣ Common Beginner Mistakes
❌ Forgetting to update variable in while loop
❌ Creating infinite loop accidentally
❌ No delay → too fast execution
❌ Using loop inside loop() unnecessarily
Example mistake:
while (i < 5) {
}
If i never changes → infinite loop.
📌 Lesson Summary
In this lesson, we learned:
-
What loops are
-
for loop structure
-
while loop structure
-
Infinite loop concept
-
How Arduino loop() works
-
break and continue
-
Real IoT examples
Loops allow systems to monitor continuously.
Without loops:
Automation is impossible.