Lesson 42: Using <= Less Than or Equal To Comparison Operator
Learn how <= includes the boundary value so conditions pass for smaller values and equal values.
Progress indicator
Lesson 42 of 57
Learning Objectives
- Understand what the less-than-or-equal operator (<=) checks.
- Use <= for inclusive upper-limit comparisons.
- Compare <= with < and choose correctly for boundary behavior.
- Use <= in if/else conditions with sensor data.
- Predict branch behavior at below, equal, and above edge values.
- Avoid common inclusive-comparison mistakes.
Concept Explanation
What is the Less Than or Equal To Operator (<=)
The <= operator checks whether the left value is smaller than or equal to the right value.
This means two cases return true: lower-than and exactly-equal.
Less Than or Equal To Syntax
if (valueA <= valueB) {
// runs when valueA is smaller than or equal to valueB
}How <= Works
- Evaluate the left and right expressions.
- Check if left is smaller than right.
- If not smaller, check equality.
- Return true if either lower-than or equal case is true.
Comparing Variables and Values
temperatureC <= maxSafeTempfor safe-range checks.batteryPercent <= 20for low-battery action at boundary.sensorValue <= LIMITwhen equal should still pass.
<= vs < (Comparison)
<passes only strictly smaller values.<=passes smaller values and equal value.
| Expression | Value = 1200 | Behavior |
|---|---|---|
value < 1200 | false | Only 1199 and below pass |
value <= 1200 | true | 1200 and below pass |
Using <= in Conditions
Use <= when the boundary value should be included in the true case. This is common in limit checks and threshold-based control.
Real embedded examples
if (motorTemp <= maxTemp): keep motor running in safe range.if (batteryDropCount <= 3): allow retry attempts up to limit.if (distanceCm <= stopDistance): trigger stop at boundary too.
When to Use <=
- Inclusive upper-limit checks.
- Safety limits where boundary counts as acceptable.
- Threshold logic where equal value should trigger action.
Edge-Value Behavior (Important)
Boundary checks are where most beginner bugs happen. Always test three points: below, exactly equal, and above.
| sensorValue | Condition | Result |
|---|---|---|
| 1199 | 1199 <= 1200 | true (if-branch) |
| 1200 | 1200 <= 1200 | true (if-branch) |
| 1201 | 1201 <= 1200 | false (else-branch) |
Example Code
This sketch turns LED ON when the value is at or below the limit.
const int LED_PIN = 2;
const int LIMIT = 1200;
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
sensorValue = analogRead(A0);
if (sensorValue <= LIMIT) {
digitalWrite(LED_PIN, HIGH);
Serial.println("At or below limit -> LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Above limit -> LED OFF");
}
Serial.print("sensorValue = ");
Serial.println(sensorValue);
delay(300);
}Example Code Explanation
const int LIMIT = 1200;defines an inclusive boundary value.sensorValue = analogRead(A0);reads the latest input sample each loop.if (sensorValue <= LIMIT)checks below-limit and equal-limit together.- If true, LED is set HIGH and a clear Serial message is printed.
- If false, value is above limit, so LED is set LOW and else message is printed.
Serial.printlines help verify exact runtime values against the limit.delay(300)slows the cycle so branch transitions are easy to observe.
Real-life analogy
A parking gate allows vehicles with height up to and including 2.0 m. That rule isheight <= 2.0. At exactly 2.0 m, the gate still allows entry.
What Happens Inside
- Left and right operands are evaluated.
- The CPU checks lower-than path and equality path.
- A boolean result (true/false) is generated.
- Compiler-generated branch instruction picks if or else block.
- GPIO state is updated so hardware reflects branch decision.
- Serial monitor confirms runtime decision with textual feedback.
- Loop repeats with new readings and re-checks the boundary.
Common Mistakes with <=
- Using
<when equal should be allowed. - Setting wrong limit value and misreading boundary behavior.
- Not testing exact boundary values like 1200 in this example.
- Comparing values from different scales without conversion.
Best Practices for <=
- Use named constants for comparison boundaries.
- Document whether the check is strict (
<) or inclusive (<=). - Test below-limit, equal-limit, and above-limit values.
- Keep threshold constants centralized so updates stay consistent.
- Use Serial logs that print both current value and limit for fast debugging.
Practice Task
- Set
LIMITto 1000 and test 999, 1000, and 1001. - Replace
<=with<and compare the 1000 boundary result. - Print both
sensorValueandLIMITeach loop. - Add
&& systemEnabledso condition only runs when enabled.
Try it now
Open the simulator workspace and test below, equal, and above boundary behavior with <=.