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
- Read the current variable value.
- Add the right-side value.
- Store the new total back into the same variable.
Updating Variables with +=
count += 1increments counters.sum += sensorValuebuilds totals.blinkDelay += 50changes timing progressively.
+= vs + and = (Comparison)
x = x + 5andx += 5do the same update.+=is shorter and easier to read in loops.
x = x + 5; // long form
x += 5; // compound formUsing += in Loops
In repeated loops, += is commonly used for counters, totals, and step-based value changes.
Real embedded examples
pulseCount += 1each time an interrupt fires.energySum += samplefor running average calculations.pwmValue += 5for 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.
| Cycle | cycleCount | blinkDelay | Update |
|---|---|---|---|
| Start | 0 | 200 | Initial values |
| After loop 1 | 1 | 300 | += applied once |
| After loop 2 | 2 | 400 | += 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
cycleCountstarts at 0 and tracks total loop cycles.blinkDelaystarts at 200 ms and controls LED ON/OFF timing.cycleCount += 1increases the cycle counter each loop.blinkDelay += stepMsmakes blinking progressively slower.- If delay gets too high, it resets to keep behavior readable.
- 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
- Current variable value is loaded from memory.
- Arithmetic addition is performed.
- Result is stored back to the same variable.
- Updated value is used in the next loop iteration.
- 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
- Change
stepMsfrom 100 to 50 and observe slower growth. - Print values before and after each
+=line. - Replace
cycleCount += 1withcycleCount += 2and compare. - 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.