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

  1. Read the current variable value.
  2. Subtract the right-side value.
  3. Store the new result back into the same variable.

Updating Variables with -=

  • count -= 1 for countdown loops.
  • budget -= cost for remaining resources.
  • blinkDelay -= 50 to speed up blink timing gradually.

-= vs - and = (Comparison)

  • x = x - 5 and x -= 5 do the same update.
  • -= keeps loop update lines shorter and cleaner.
x = x - 5;   // long form
x -= 5;      // compound form

Using -= in Loops

In repeated loops, -= is useful for decreasing counters, timers, and step-down values.

Real embedded examples

  • remainingSeconds -= 1 in a countdown timer.
  • batteryPercent -= drainRate in power simulations.
  • pwmValue -= 10 for 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.

CyclecycleCountblinkDelayUpdate
Start0900Initial values
After loop 11800-= applied once
After loop 22700-= 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

  1. blinkDelay starts high (900 ms) and controls blink speed.
  2. cycleCount += 1 tracks loop progress every cycle.
  3. blinkDelay -= stepMs reduces delay in fixed steps.
  4. As delay gets smaller, blink becomes faster.
  5. When delay goes below minimum threshold, it resets to 900.
  6. 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

  1. Current variable value is loaded from memory.
  2. Subtraction is performed with right-side operand.
  3. Result is written back to the same variable.
  4. Updated value is used in the next loop cycle.
  5. 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

  1. Change stepMs from 100 to 50 and compare blink speed change.
  2. Print values before and after blinkDelay -= stepMs.
  3. Set reset threshold to 300 instead of 200 and observe behavior.
  4. 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.

Run in Simulator