Lesson 40: Using >= Greater Than or Equal To Comparison Operator
Learn how >= handles inclusive thresholds so conditions pass for equal values and larger values.
Progress indicator
Lesson 40 of 57
Learning Objectives
- Understand what the greater-than-or-equal operator (>=) does.
- Use >= for inclusive threshold checks.
- Compare >= with > and choose correctly.
- Use >= safely in if/else conditions.
- Understand inclusive threshold behavior in real sensor systems.
- Avoid common mistakes in inclusive comparisons.
Concept Explanation
What is the Greater Than or Equal To Operator (>=)
The >= operator checks whether the left value is greater than or equal to the right value.
Condition is true for both cases: larger value or exact equal value.
Greater Than or Equal To Syntax
if (valueA >= valueB) {
// runs when valueA is equal to or larger than valueB
}How >= Works
- Evaluate left and right expressions.
- Check if left is larger than right.
- If not larger, check if both are equal.
- Return true if either condition is satisfied.
This inclusive check is ideal when reaching the exact limit should also trigger the action.
Comparing Variables and Values
sensorValue >= THRESHOLDfor inclusive threshold checks.batteryPercent >= 20to allow feature when minimum is reached.counter >= limitto trigger actions at or beyond target.
>= vs > (Comparison)
>passes only when left is strictly larger.>=passes when left is equal or larger.
| Expression | Value = 2000 | Behavior |
|---|---|---|
value > 2000 | false | Only 2001 and above pass |
value >= 2000 | true | 2000 and above pass |
if (value > 2000) { /* 2001+ */ }
if (value >= 2000) { /* 2000+ */ }Using >= in Conditions
Use >= when hitting the exact threshold should also trigger behavior. This is common in safety limits and minimum requirements.
Real examples
if (batteryPercent >= 20)for minimum battery operation.if (temperatureC >= maxSafeTemp)for overheat protection.if (score >= passMark)for pass/fail logic.
When to Use >=
- Minimum pass conditions (at least X).
- Inclusive threshold alerts.
- Counter checks for reaching or passing milestones.
Example Code
This sketch turns LED ON when sensor value is at or above the threshold.
const int LED_PIN = 2;
const int THRESHOLD = 2000;
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
sensorValue = analogRead(A0);
if (sensorValue >= THRESHOLD) {
digitalWrite(LED_PIN, HIGH);
Serial.println("At or above threshold -> LED ON");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("Below threshold -> LED OFF");
}
Serial.print("sensorValue = ");
Serial.println(sensorValue);
delay(300);
}Example Code Explanation
- Read sensor value from analog input.
- Compare with
sensorValue >= THRESHOLD. - LED turns ON when value equals threshold or goes above it.
- Else branch runs only when value is lower than threshold.
- Serial output prints current value for tuning and debugging.
delay(300)keeps output transitions easy to observe.
Real-life analogy
Entry is allowed when age is 18 or older. Here, 18 should pass, so the condition is age >= 18, not age > 18.
What Happens Inside
- Operands are loaded into comparison logic.
- Hardware checks greater-than path and equality path.
- Boolean result is generated from combined checks.
- Program selects corresponding if/else branch.
- Loop repeats with updated sensor value and re-evaluates the inclusive check.
Common Mistakes with >=
- Using
>when equal-value case should pass. - Setting wrong threshold value and misreading behavior.
- Not logging values, making inclusive comparison bugs hard to detect.
- Confusing strict limits with inclusive limits in system requirements.
Best Practices for >=
- Use named constants for threshold values.
- Document whether your check is strict (>) or inclusive (>=).
- Print compared values while tuning hardware thresholds.
- Document threshold intent in comments (strict vs inclusive).
Practice Task
- Set
THRESHOLDto a value you can reach easily and test equal-case behavior. - Replace
>=with>and compare results at exact threshold. - Print both threshold and sensor value each loop for debugging.
- Add an additional condition like
systemEnabled == truewith&&.
Try it now
Open the simulator workspace and test inclusive threshold behavior with >=.