Operators

Lesson 43: Using != Not Equal To Comparison Operator

Learn how != checks whether two values are different and helps trigger actions when states change.

Progress indicator

Lesson 43 of 57

Learning Objectives

  • Understand what the not-equal operator (!=) checks.
  • Use != to detect value or state changes.
  • Compare != with == and select the right condition.
  • Use != safely in if conditions for input handling.
  • Understand change-detection flow using current and previous state.
  • Avoid common mistakes when comparing values.

Concept Explanation

What is the Not Equal To Operator (!=)

The != operator checks whether two values are different.

It returns true only when the left value and right value are not the same.

Not Equal To Syntax

if (valueA != valueB) {
  // runs when values are different
}

How != Works

  1. Read both values to compare.
  2. Check if they are exactly the same.
  3. If same, result is false.
  4. If different, result is true.

Comparing Variables and Values

  • buttonState != lastButtonState to detect input change.
  • sensorValue != previousValue to log updates only when needed.
  • mode != expectedMode to run correction logic.

!= vs == (Comparison)

  • == is true when two values are equal.
  • != is true when two values are different.
ExpressionValue A=1, B=1Value A=1, B=0
A == Btruefalse
A != Bfalsetrue

Using != in Conditions

Use != when you want code to run on change or mismatch instead of exact match.

Real embedded examples

  • if (buttonState != lastButtonState): detect press/release events.
  • if (sensorMode != expectedMode): reconfigure subsystem.
  • if (newValue != oldValue): send Serial log only on change.

When to Use !=

  • Input state change detection.
  • Mismatch checks in validation logic.
  • Avoiding repeated actions when value has not changed.

Change Detection Table

This is the core idea behind many button and event-driven programs.

buttonStatelastButtonStatebuttonState != lastButtonStateResult
HIGHHIGHfalseNo change action
LOWHIGHtrueChange detected
LOWLOWfalseNo change action

Example Code

This sketch toggles LED only when button state changes.

const int LED_PIN = 2;
int buttonState = HIGH;
int lastButtonState = HIGH;

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

void loop() {
  buttonState = digitalRead(0);

  if (buttonState != lastButtonState) {
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
    Serial.println("Button state changed");
    lastButtonState = buttonState;
  }

  delay(50);
}

Example Code Explanation

  1. buttonState stores the current reading from digitalRead(0).
  2. lastButtonState stores the previous loop reading for comparison.
  3. if (buttonState != lastButtonState) checks whether state changed.
  4. On change, LED toggles and Serial prints a change message.
  5. lastButtonState = buttonState updates memory so next loop compares correctly.
  6. delay(50) helps reduce repeated rapid triggers from button bounce.

Real-life analogy

A security system sends an alert only when door state changes. If door stays closed, no alert is sent. That is exactly a currentState != previousState pattern.

What Happens Inside

  1. CPU evaluates both operands in the comparison.
  2. Comparator checks value mismatch.
  3. Boolean result selects if-branch or skip path.
  4. If branch executes side effects: LED toggle and Serial output.
  5. State memory is updated after handling change.
  6. Loop repeats and waits for next mismatch event.

Common Mistakes with !=

  • Using = assignment instead of != comparison.
  • Forgetting to update previous state variable after change.
  • Ignoring switch bounce and reading noisy rapid transitions.
  • Comparing values with incompatible meaning (for example raw ADC vs mapped percent).

Best Practices for !=

  • Use clear names like currentState and lastState.
  • Update previous state right after processing change logic.
  • Use short debounce delay or proper debounce logic for buttons.
  • Log both current and previous values when debugging change detection.
  • Keep update order correct: compare first, then copy current into previous.

Practice Task

  1. Print both buttonState and lastButtonState every loop.
  2. Toggle LED only when release is detected (LOW to HIGH transition).
  3. Increase delay(50) to 100 and compare behavior.
  4. Add a counter that increments only when state changes.

Try it now

Open the simulator workspace and test state-change detection behavior using !=.

Run in Simulator