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
- Read both values to compare.
- Check if they are exactly the same.
- If same, result is false.
- If different, result is true.
Comparing Variables and Values
buttonState != lastButtonStateto detect input change.sensorValue != previousValueto log updates only when needed.mode != expectedModeto run correction logic.
!= vs == (Comparison)
==is true when two values are equal.!=is true when two values are different.
| Expression | Value A=1, B=1 | Value A=1, B=0 |
|---|---|---|
A == B | true | false |
A != B | false | true |
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.
| buttonState | lastButtonState | buttonState != lastButtonState | Result |
|---|---|---|---|
| HIGH | HIGH | false | No change action |
| LOW | HIGH | true | Change detected |
| LOW | LOW | false | No 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
buttonStatestores the current reading fromdigitalRead(0).lastButtonStatestores the previous loop reading for comparison.if (buttonState != lastButtonState)checks whether state changed.- On change, LED toggles and Serial prints a change message.
lastButtonState = buttonStateupdates memory so next loop compares correctly.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
- CPU evaluates both operands in the comparison.
- Comparator checks value mismatch.
- Boolean result selects if-branch or skip path.
- If branch executes side effects: LED toggle and Serial output.
- State memory is updated after handling change.
- 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
currentStateandlastState. - 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
- Print both
buttonStateandlastButtonStateevery loop. - Toggle LED only when release is detected (LOW to HIGH transition).
- Increase
delay(50)to 100 and compare behavior. - Add a counter that increments only when state changes.
Try it now
Open the simulator workspace and test state-change detection behavior using !=.