Lesson 48: Using %= Compound Remainder Operator
Learn how %= updates a value with remainder logic in one step, useful for wrap-around and periodic behavior.
Progress indicator
Lesson 48 of 57
Learning Objectives
- Understand what the compound remainder operator (%=) does.
- Use %= to keep numbers inside a repeating range.
- Compare %= with writing % and = separately.
- Apply %= inside loops for periodic events.
- Avoid common errors such as using 0 as divisor.
- Read real loop states and predict when events will fire.
Concept Explanation
What is the Compound Remainder Operator (%=)
The %= operator calculates remainder and stores the new remainder value back into the same variable.
It is a short way to write value = value % divisor.
Compound Remainder Syntax
index %= 3;
counter %= maxCount;How %= Works
- Read current variable value.
- Compute remainder after division by right-side value.
- Store remainder into the same variable.
Quick numeric view: if value is 10 and divisor is 3, then remainder is 1, so value %= 3 makes value become 1.
Updating Variables with %=
index %= 4keeps index between 0 and 3.stepCount %= 10makes a repeating 0-9 counter.phase %= 2toggles between two states.
%= vs % and = (Comparison)
x = x % 3andx %= 3produce the same result.%=is shorter and easier to read in loop updates.
x = x % 3; // long form
x %= 3; // compound formUsing %= in Loops
In loops, %= is useful for repeating patterns. It keeps values wrapped in a fixed range instead of growing forever.
Real embedded examples
- Blink LED every third loop cycle.
- Rotate through 3 menu pages: 0 -> 1 -> 2 -> 0.
- Repeat sensor sampling slots in fixed windows.
When to Use %=
- Wrap-around counters.
- Periodic actions (every N cycles).
- Index control inside fixed-size ranges.
Data Type Notes
%and%=are intended for integer types.- For
intvalues, results stay in integer range only. - If divisor is positive, result stays between 0 and divisor-1 in this lesson flow.
- Use clear integer variables for counters and phase indexes.
Example Code
This sketch uses %= to repeat a cycle index and blink LED at a fixed interval.
const int LED_PIN = 2;
int cycleIndex = 0;
int cycleMod = 3;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
cycleIndex += 1;
cycleIndex %= cycleMod;
if (cycleIndex == 0) {
digitalWrite(LED_PIN, HIGH);
delay(400);
digitalWrite(LED_PIN, LOW);
delay(400);
Serial.println("Cycle event: LED blink");
} else {
delay(200);
Serial.println("No blink this cycle");
}
}Example Code Explanation
cycleIndexincreases each loop to track progression.cycleIndex %= cycleModwraps value back into 0..2 range.- When remainder becomes 0, LED blink event runs.
- For remainder 1 or 2, code takes the non-blink path.
- Serial messages show whether current cycle triggered the event.
Real-life analogy
Think of a traffic signal timer with 3 phases. After phase 2, it returns to phase 0 again. This repeating wrap behavior is exactly what %= helps build.
What Happens Inside
- Variable value is loaded from memory.
- Division remainder operation is executed.
- Only remainder is kept, quotient is discarded.
- Remainder is written back to the same variable.
- Next loop uses wrapped value for periodic decisions.
| Loop | After +1 | After %= 3 | LED Event |
|---|---|---|---|
| 1 | 1 | 1 | No |
| 2 | 2 | 2 | No |
| 3 | 3 | 0 | Yes |
Common Mistakes with %=
- Using 0 as divisor (invalid operation).
- Expecting %= to return quotient instead of remainder.
- Using float variables with % (remainder operator is for integers).
- Choosing wrong modulus value and getting unexpected cycle length.
Best Practices for %=
- Use clear names like
cycleIndexandcycleMod. - Always ensure divisor is greater than zero.
- Use Serial logs to verify repeating pattern during practice.
- Combine %= with if conditions for clean periodic events.
- Keep modulus value in a named variable so lesson edits stay simple.
Practice Task
- Change
cycleModfrom 3 to 4 and check when blink occurs. - Print
cycleIndexbefore and after%=in each loop. - Trigger a second event when
cycleIndex == 2and compare patterns. - Try a larger modulus (like 7) and verify periodic timing logic.
Try it now
Open the simulator workspace and observe how %= creates repeating cycle behavior in the loop.