Operators

Lesson 39: Using > Greater Than Comparison Operator

Learn how > compares numeric values and helps trigger actions when values exceed thresholds.

Progress indicator

Lesson 39 of 57

Learning Objectives

  • Understand what the greater-than operator (>) does.
  • Use > to compare variables and constant values.
  • Understand > vs >= and choose the correct comparison.
  • Use > in if/else conditions for threshold-based decisions.
  • Understand strict-threshold behavior in real Arduino signals.
  • Avoid common mistakes in greater-than comparisons.

Concept Explanation

What is the Greater Than Operator (>)

The > operator checks whether the left value is larger than the right value.

If left is bigger, result is true. Otherwise, result is false.

Greater Than Syntax

if (valueA > valueB) {
  // runs when valueA is bigger
}

How > Works

  1. Evaluate left expression value.
  2. Evaluate right expression value.
  3. Compare both values numerically.
  4. Return true if left is strictly larger.

Strictly larger means equal values do not pass the condition. For equal values, use >= when needed.

Comparing Variables and Values

  • sensorValue > 2000 checks threshold crossing.
  • currentTemp > maxTemp checks limit exceed condition.
  • counter > 10 checks progression beyond a point.

> vs >= (Comparison)

  • > means strictly greater than.
  • >= means greater than or equal to.
ExpressionValue = 2000Meaning
value > 2000falseOnly values 2001+ pass
value >= 2000trueValue 2000 and above pass
if (score > 80)   { /* only 81+ */ }
if (score >= 80)  { /* 80 and above */ }

Using > in Conditions

In Arduino projects, > is often used for threshold decisions such as turning on outputs when sensor readings go above a limit.

Real examples

  • if (temperature > maxAllowed) for overheat warning.
  • if (distanceCm > safeDistance) for motion decisions.
  • if (batteryPercent > 20) to allow high-power actions.

When to Use >

  • Temperature or light threshold alerts.
  • Detecting value growth beyond safe range.
  • Flow control after counters exceed target values.

Example Code

This sketch turns LED ON only when the sensor value is greater than the threshold.

const int LED_PIN = 2;
const int LDR_PIN = A0;
const int THRESHOLD = 2000;

int sensorValue = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  sensorValue = analogRead(LDR_PIN);

  if (sensorValue > THRESHOLD) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Sensor is above threshold -> LED ON");
  } else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("Sensor is not above threshold -> LED OFF");
  }

  Serial.print("sensorValue = ");
  Serial.println(sensorValue);
  delay(300);
}

Example Code Explanation

  1. Read sensor value from analog input.
  2. sensorValue > THRESHOLD compares current reading with limit.
  3. When true, LED turns ON and serial message confirms threshold exceeded.
  4. When false, LED turns OFF and message shows condition not met.
  5. Serial prints current value for real-time debugging.
  6. delay(300) keeps readings easy to observe in Serial Monitor.

Real-life analogy

A speed alarm triggers only when speed is greater than the limit, not when it is exactly equal to the limit.

What Happens Inside

  1. CPU loads both operands into comparison logic.
  2. Comparator checks if left operand is numerically larger.
  3. Comparator returns a boolean result.
  4. Program branches to matching if/else block using that result.
  5. Loop repeats with new sensor values, producing dynamic threshold behavior.

Common Mistakes with >

  • Using > when >= was required.
  • Comparing wrong variable or wrong threshold value.
  • Not printing values during debugging, making condition errors hard to detect.
  • Choosing > when inclusive threshold behavior was required.

Best Practices for >

  • Use clear constant names like THRESHOLD.
  • Print compared values while tuning thresholds.
  • Double-check whether strict or inclusive comparison is intended.
  • Keep thresholds in named constants so tuning is easy and safe.

Practice Task

  1. Change THRESHOLD to a lower value and observe LED behavior.
  2. Replace one > with >= and compare outcome.
  3. Print both threshold and sensor value each cycle for debugging.
  4. Add a second condition with && (for example, only if system is enabled).

Try it now

Open the simulator workspace and test threshold comparisons with live values.

Run in Simulator