Operators

Lesson 41: Using < Less Than Comparison Operator

Learn how < checks strict lower-than conditions and helps you trigger actions before a value reaches a limit.

Progress indicator

Lesson 41 of 57

Learning Objectives

  • Understand what the less-than operator (<) checks.
  • Use < for strict upper-limit comparisons.
  • Compare < with <= and choose the correct one.
  • Apply < in if/else conditions with sensor values.
  • Predict branch behavior at edge values (below, equal, above).
  • Avoid common comparison mistakes in Arduino code.

Concept Explanation

What is the Less Than Operator (<)

The < operator checks if the left value is strictly smaller than the right value.

If both values are equal, the result is false because < does not include equality.

Less Than Syntax

if (valueA < valueB) {
  // runs only when valueA is smaller than valueB
}

How < Works

  1. Evaluate the left and right expressions.
  2. Compare their numeric values.
  3. Return true only if left is smaller than right.
  4. Return false when left is equal to or greater than right.

Comparing Variables and Values

  • temperatureC < minTemp for cold alerts.
  • batteryPercent < 20 for low-battery warnings.
  • sensorValue < LIMIT for under-threshold actions.

< vs <= (Comparison)

  • < means strictly smaller.
  • <= means smaller or equal.
ExpressionValue = 1200Behavior
value < 1200falseOnly 1199 and below pass
value <= 1200true1200 and below pass

Using < in Conditions

Use < when your logic should run only before a limit is reached. This is useful in warning logic and pre-limit control behavior.

Real embedded examples

  • if (tankLevel < 10): show low-water warning.
  • if (batteryPercent < 15): reduce power usage mode.
  • if (lightValue < darkLimit): turn hallway LED ON.

When to Use <

  • Low threshold checks (below minimum).
  • Pre-limit warnings before reaching a boundary.
  • Strict comparisons where equal should not pass.

Edge-Value Behavior (Important)

Most logic bugs happen at boundary values. Always test three cases: just below limit, exactly at limit, and just above limit.

sensorValueConditionResult
11991199 < 1200true (if-branch)
12001200 < 1200false (else-branch)
12011201 < 1200false (else-branch)

Example Code

This sketch turns LED ON only when the sensor value is 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("Value is below limit -> LED ON");
  } else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("Value reached or exceeded limit -> LED OFF");
  }

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

Example Code Explanation

  1. const int LIMIT = 1200; defines the strict boundary for low-value behavior.
  2. sensorValue = analogRead(A0); gets the latest analog reading each loop.
  3. if (sensorValue < LIMIT) checks only values lower than 1200.
  4. If true, LED turns ON and Serial prints a below-limit message.
  5. If false (equal or greater), LED turns OFF and Serial prints the else message.
  6. Serial.print + Serial.println output live values so you can verify branch decisions.
  7. delay(300) slows updates to keep transitions readable in simulator/monitor.

Real-life analogy

Think of a speed warning set at 60. Rule: warn only when speed is less than 60 for slow-speed guidance. At exactly 60, condition speed < 60 is false.

What Happens Inside

  1. Both comparison operands are evaluated.
  2. The MCU comparison unit checks whether left is strictly lower than right.
  3. A boolean result is produced (true/false).
  4. The compiler-generated branch instruction chooses if or else block.
  5. GPIO write updates LED state to reflect the selected branch.
  6. Serial output confirms runtime decision for debugging.
  7. The loop repeats and checks again with new input values.

Common Mistakes with <

  • Using < when equality should also be allowed.
  • Mixing < and > accidentally in threshold checks.
  • Skipping Serial prints and guessing comparison behavior.
  • Comparing values with different scales (for example raw ADC vs mapped percentage).

Best Practices for <

  • Use clear constant names like MIN_TEMP or LIMIT.
  • Document whether the boundary should include equality.
  • Test around edge values: just below, exactly equal, and just above.
  • Keep threshold constants in one place so changes are safe and predictable.
  • Use clear Serial logs that include both current value and limit.

Practice Task

  1. Set LIMIT to 1000 and test values 999, 1000, and 1001.
  2. Change < to <= and note behavior change at exactly 1000.
  3. Print both sensorValue and LIMIT each loop.
  4. Add a second condition with && to require system enable flag.

Try it now

Open the simulator workspace and test below-limit and exact-limit behavior using <.

Run in Simulator