Operators

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

  1. Read current variable value.
  2. Compute remainder after division by right-side value.
  3. 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 %= 4 keeps index between 0 and 3.
  • stepCount %= 10 makes a repeating 0-9 counter.
  • phase %= 2 toggles between two states.

%= vs % and = (Comparison)

  • x = x % 3 and x %= 3 produce the same result.
  • %= is shorter and easier to read in loop updates.
x = x % 3;   // long form
x %= 3;      // compound form

Using %= 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 int values, 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

  1. cycleIndex increases each loop to track progression.
  2. cycleIndex %= cycleMod wraps value back into 0..2 range.
  3. When remainder becomes 0, LED blink event runs.
  4. For remainder 1 or 2, code takes the non-blink path.
  5. 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

  1. Variable value is loaded from memory.
  2. Division remainder operation is executed.
  3. Only remainder is kept, quotient is discarded.
  4. Remainder is written back to the same variable.
  5. Next loop uses wrapped value for periodic decisions.
LoopAfter +1After %= 3LED Event
111No
222No
330Yes

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 cycleIndex and cycleMod.
  • 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

  1. Change cycleMod from 3 to 4 and check when blink occurs.
  2. Print cycleIndex before and after %= in each loop.
  3. Trigger a second event when cycleIndex == 2 and compare patterns.
  4. 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.

Run in Simulator