Operators
Lesson 45: Using -= Compound Subtraction Operator
Learn how -= updates a variable by subtracting a value in one step, useful for countdowns and decreasing timers.
Progress indicator
Lesson 45 of 57
Learning Objectives
- Understand what the compound subtraction operator (-=) does.
- Use -= to update variables cleanly in one step.
- Compare -= with using - and = separately.
- Use -= in loop-based countdown and timing logic.
- Understand how -= changes variable state across repeated loop cycles.
- Avoid common mistakes when repeatedly subtracting values.
Concept Explanation
What is the Compound Subtraction Operator (-=)
The -= operator subtracts a value from a variable and stores the result back into that same variable.
Compound Subtraction Syntax
counter -= 1;
delayMs -= stepMs;How -= Works
- Read the current variable value.
- Subtract the right-side value.
- Store the new result back into the same variable.
Updating Variables with -=
count -= 1for countdown loops.budget -= costfor remaining resources.blinkDelay -= 50to speed up blink timing gradually.
-= vs - and = (Comparison)
x = x - 5andx -= 5do the same update.-=keeps loop update lines shorter and cleaner.
x = x - 5; // long form
x -= 5; // compound formUsing -= in Loops
In repeated loops, -= is useful for decreasing counters, timers, and step-down values.
Real embedded examples
remainingSeconds -= 1in a countdown timer.batteryPercent -= drainRatein power simulations.pwmValue -= 10for smooth fade-out control.
When to Use -=
- Countdown logic.
- Reducing limits over time.
- Step-down control for delays and thresholds.
Loop State Table
This shows how values change when -= runs every loop.
| Cycle | cycleCount | blinkDelay | Update |
|---|---|---|---|
| Start | 0 | 900 | Initial values |
| After loop 1 | 1 | 800 | -= applied once |
| After loop 2 | 2 | 700 | -= applied twice |
Example Code
This sketch uses -= to reduce blink delay after each cycle.
const int LED_PIN = 2;
int blinkDelay = 900;
int stepMs = 100;
int cycleCount = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(blinkDelay);
digitalWrite(LED_PIN, LOW);
delay(blinkDelay);
cycleCount += 1;
blinkDelay -= stepMs;
if (blinkDelay < 200) {
blinkDelay = 900;
}
Serial.print("cycleCount = ");
Serial.print(cycleCount);
Serial.print(", blinkDelay = ");
Serial.println(blinkDelay);
}Example Code Explanation
blinkDelaystarts high (900 ms) and controls blink speed.cycleCount += 1tracks loop progress every cycle.blinkDelay -= stepMsreduces delay in fixed steps.- As delay gets smaller, blink becomes faster.
- When delay goes below minimum threshold, it resets to 900.
- Serial output confirms each update in real time.
Real-life analogy
Think of a countdown clock where each second decreases remaining time:secondsLeft -= 1. The value moves downward every tick.
What Happens Inside
- Current variable value is loaded from memory.
- Subtraction is performed with right-side operand.
- Result is written back to the same variable.
- Updated value is used in the next loop cycle.
- Repeated loops create a continuous step-down pattern.
Common Mistakes with -=
- Using uninitialized variables before applying
-=. - Subtracting too much and creating negative or invalid values.
- Forgetting lower-bound reset logic in repeated loops.
- Using decrement steps that are too large for smooth transitions.
Best Practices for -=
- Use clear names for decreasing counters and timers.
- Set safe minimum bounds before values go too low.
- Log updated values with Serial while validating loop behavior.
- Keep decrement steps in named variables like
stepMs. - Define and enforce a clear minimum boundary for safe operation.
Practice Task
- Change
stepMsfrom 100 to 50 and compare blink speed change. - Print values before and after
blinkDelay -= stepMs. - Set reset threshold to 300 instead of 200 and observe behavior.
- Add a second variable that counts how many resets happened.
Try it now
Open the simulator workspace and observe how -= decreases loop values each cycle.