Operators

Lesson 44: Using += Compound Addition Operator

Learn how += updates a variable by adding a value in one step, making counter and loop code cleaner.

Progress indicator

Lesson 44 of 57

Learning Objectives

  • Understand what the compound addition operator (+=) does.
  • Use += to update variables cleanly.
  • Compare += with using + and = separately.
  • Use += in loop-based counters and timing logic.
  • Understand how += affects variable state across repeated loop cycles.
  • Avoid common mistakes with repeated additions.

Concept Explanation

What is the Compound Addition Operator (+=)

The += operator adds a value to a variable and stores the result back into the same variable.

Compound Addition Syntax

counter += 1;
delayMs += stepMs;

How += Works

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

Updating Variables with +=

  • count += 1 increments counters.
  • sum += sensorValue builds totals.
  • blinkDelay += 50 changes timing progressively.

+= vs + and = (Comparison)

  • x = x + 5 and x += 5 do the same update.
  • += is shorter and easier to read in loops.
x = x + 5;   // long form
x += 5;      // compound form

Using += in Loops

In repeated loops, += is commonly used for counters, totals, and step-based value changes.

Real embedded examples

  • pulseCount += 1 each time an interrupt fires.
  • energySum += sample for running average calculations.
  • pwmValue += 5 for smooth LED brightness ramping.

When to Use +=

  • Counting events.
  • Accumulating totals.
  • Gradually changing timing or thresholds.

Loop State Table

This shows how variables evolve when += runs every loop cycle.

CyclecycleCountblinkDelayUpdate
Start0200Initial values
After loop 11300+= applied once
After loop 22400+= applied twice

Example Code

This sketch uses += to increase a counter and blink delay after each cycle.

const int LED_PIN = 2;
int blinkDelay = 200;
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 > 800) {
    blinkDelay = 200;
  }

  Serial.print("cycleCount = ");
  Serial.print(cycleCount);
  Serial.print(", blinkDelay = ");
  Serial.println(blinkDelay);
}

Example Code Explanation

  1. cycleCount starts at 0 and tracks total loop cycles.
  2. blinkDelay starts at 200 ms and controls LED ON/OFF timing.
  3. cycleCount += 1 increases the cycle counter each loop.
  4. blinkDelay += stepMs makes blinking progressively slower.
  5. If delay gets too high, it resets to keep behavior readable.
  6. Serial output shows both values so you can verify updates.

Real-life analogy

Think of walking stairs: each step adds one floor to your current floor.floor += 1 is a direct way to move upward from current state.

What Happens Inside

  1. Current variable value is loaded from memory.
  2. Arithmetic addition is performed.
  3. Result is stored back to the same variable.
  4. Updated value is used in the next loop iteration.
  5. Repeated loops create a running progression pattern.

Common Mistakes with +=

  • Forgetting variable initialization before using +=.
  • Using too large increments and causing unintended overflow.
  • Updating the wrong variable inside loops.
  • Forgetting reset conditions, causing values to grow beyond intended range.

Best Practices for +=

  • Use clear names for counters and accumulators.
  • Keep increment step values in constants or clearly named variables.
  • Print variable updates while debugging loop behavior.
  • Use boundary checks and reset logic when values should wrap.
  • Keep increments small and predictable for beginner-friendly debugging.

Practice Task

  1. Change stepMs from 100 to 50 and observe slower growth.
  2. Print values before and after each += line.
  3. Replace cycleCount += 1 with cycleCount += 2 and compare.
  4. Add a second counter for total LED ON events using +=.

Try it now

Open the simulator workspace and observe how += updates counters and delay values each cycle.

Run in Simulator