📘 Operators
🎯 Lesson Objective
By the end of this lesson, students will:
-
Understand what operators are
-
Learn arithmetic operators
-
Learn comparison operators
-
Learn logical operators
-
Understand assignment operators
-
Use operators to build automation logic
1️⃣ What is an Operator?
An operator is a symbol that performs an operation on variables or values.
Example:
Here:+ is an operator.
It performs addition.
Operators allow the program to process data.
2️⃣ Types of Operators in Arduino / C++
There are 4 main types we will focus on:
1️⃣ Arithmetic Operators
2️⃣ Comparison Operators
3️⃣ Logical Operators
4️⃣ Assignment Operators
🔹 3️⃣ Arithmetic Operators
Used for mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | a + b |
| – | Subtraction | a – b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
📌 Example 1 – Basic Math
int a = 10;
int b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int division = a / b; // 3
int remainder = a % b; // 1
Important:
Integer division removes decimals.
10 / 3 = 3 (not 3.33)
If you want decimal result → use float.
📌 Real IoT Example
Water Tank Level Percentage:
int level = 300;
int maxLevel = 500;
int percentage = (level * 100) / maxLevel;
Operators allow real-time calculation.
🔹 4️⃣ Assignment Operators
Used to assign values.
🔹 Basic Assignment
= means “store value”.
🔹 Short Assignment Operators
| Operator | Meaning |
|---|---|
| += | Add and assign |
| -= | Subtract and assign |
| *= | Multiply and assign |
| /= | Divide and assign |
📌 Example
int counter = 0;
counter += 1; // counter = counter + 1
counter -= 2;
Very useful in loops.
🔹 5️⃣ Comparison Operators
Used for decision-making.
They compare values and return:
true or false
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater or equal |
| <= | Less or equal |
📌 Example
int temperature = 32;
if (temperature > 30) {
Serial.println("AC ON");
}
Comparison returns:
true → Execute block
false → Skip block
⚠️ Important
Do not confuse:
= → Assignment== → Comparison
Wrong:
Correct:
🔹 6️⃣ Logical Operators
Used to combine multiple conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| ! | NOT |
🔹 AND Operator (&&)
Both conditions must be true.
Example:
if (temperature > 30 && gasLevel > 500)
Means:
Temperature must be high
AND
Gas level must be high
🔹 OR Operator (||)
At least one condition must be true.
Example:
Either condition triggers action.
🔹 NOT Operator (!)
Reverses condition.
Example:
Means:
If pump is NOT ON.
🔟 Combining Operators – Real IoT Example
Smart Home Logic:
if (manualMode == false && temperature > setPoint) {
digitalWrite(relayPin, HIGH);
}
Breakdown:
manualMode must be false
AND
temperature must be above setPoint
Then turn AC ON.
This is real automation logic.
1️⃣1️⃣ Order of Operations
Mathematical operations follow order:
-
Parentheses
-
Multiplication / Division
-
Addition / Subtraction
Example:
Result = 14
Not 20
Use parentheses for clarity:
Result = 20
1️⃣2️⃣ Modulus Operator (%) – Useful in IoT
Returns remainder.
Example:
Useful for:
-
Blinking patterns
-
Repeating sequences
-
Time-based logic
1️⃣3️⃣ Increment & Decrement Operators
Shortcut operators:
| Operator | Meaning |
|---|---|
| ++ | Increase by 1 |
| — | Decrease by 1 |
Example:
counter—;
Very useful in loops.
1️⃣4️⃣ Real Debug Example
int gasValue = 800;
if (gasValue >= 700) {
Serial.println("Gas Alert!");
}
Comparison operator triggers alert.
Operators make sensors meaningful.
1️⃣5️⃣ Common Beginner Mistakes
❌ Using = instead of ==
❌ Forgetting parentheses
❌ Mixing && and || incorrectly
❌ Integer division mistake
❌ Not understanding operator priority
Always test with Serial Monitor.
📌 Lesson Summary
In this lesson, we learned:
-
What operators are
-
Arithmetic operators
-
Assignment operators
-
Comparison operators
-
Logical operators
-
Real IoT logic building
-
Common mistakes
Operators are the foundation of automation.
Without operators:
No decision-making.