Operators

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

  1. Evaluate left and right expressions.
  2. Check if left is larger than right.
  3. If not larger, check if both are equal.
  4. 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 >= THRESHOLD for inclusive threshold checks.
  • batteryPercent >= 20 to allow feature when minimum is reached.
  • counter >= limit to trigger actions at or beyond target.

>= vs > (Comparison)

  • > passes only when left is strictly larger.
  • >= passes when left is equal or larger.
ExpressionValue = 2000Behavior
value > 2000falseOnly 2001 and above pass
value >= 2000true2000 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

  1. Read sensor value from analog input.
  2. Compare with sensorValue >= THRESHOLD.
  3. LED turns ON when value equals threshold or goes above it.
  4. Else branch runs only when value is lower than threshold.
  5. Serial output prints current value for tuning and debugging.
  6. 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

  1. Operands are loaded into comparison logic.
  2. Hardware checks greater-than path and equality path.
  3. Boolean result is generated from combined checks.
  4. Program selects corresponding if/else branch.
  5. 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

  1. Set THRESHOLD to a value you can reach easily and test equal-case behavior.
  2. Replace >= with > and compare results at exact threshold.
  3. Print both threshold and sensor value each loop for debugging.
  4. Add an additional condition like systemEnabled == true with &&.

Try it now

Open the simulator workspace and test inclusive threshold behavior with >=.

Run in Simulator