Operators

Lesson 46: Using *= Compound Multiplication Operator

Learn how *= updates a variable by multiplying its current value in one step, useful for scaling values.

Progress indicator

Lesson 46 of 57

Learning Objectives

  • Understand what the compound multiplication operator (*=) does.
  • Use *= to scale variables in one step.
  • Compare *= with using * and = separately.
  • Use *= in loop-based growth patterns.
  • Understand how *= changes values exponentially across loop cycles.
  • Avoid common mistakes when repeatedly multiplying values.

Concept Explanation

What is the Compound Multiplication Operator (*=)

The *= operator multiplies a variable by another value and stores the new result back into the same variable.

Compound Multiplication Syntax

value *= 2;
gain *= scaleFactor;

How *= Works

  1. Read current variable value.
  2. Multiply by right-side operand.
  3. Store result back in the same variable.

Updating Variables with *=

  • value *= 2 doubles value each cycle.
  • pwm *= 2 quickly scales brightness steps.
  • delayMs *= factor increases timing exponentially.

*= vs * and = (Comparison)

  • x = x * 3 and x *= 3 do the same update.
  • *= is shorter and clearer in repeated loops.
x = x * 3;   // long form
x *= 3;      // compound form

Using *= in Loops

Use *= when values need to grow by scale factors each loop.

Real embedded examples

  • sampleWindow *= 2 for adaptive timing intervals.
  • gain *= 2 while increasing amplifier sensitivity in steps.
  • pwmStep *= factor for fast scaling demos.

When to Use *=

  • Scaling values quickly.
  • Exponential growth examples.
  • Loop-based gain adjustments.

Loop Growth Table

This shows how blinkDelay changes with *= 2 each cycle.

CycleblinkDelayUpdate
Start100Initial value
After loop 1200100 *= 2
After loop 2400200 *= 2
After loop 3800400 *= 2

Example Code

This sketch uses *= to multiply blink delay after each cycle.

const int LED_PIN = 2;
int blinkDelay = 100;
int factor = 2;
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 *= factor;

  if (blinkDelay > 800) {
    blinkDelay = 100;
  }

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

Example Code Explanation

  1. blinkDelay starts at 100 ms and controls blink timing.
  2. cycleCount += 1 increments each loop for progress tracking.
  3. blinkDelay *= factor multiplies delay by 2 on every cycle.
  4. This creates fast growth: 100, 200, 400, 800, ...
  5. Reset logic prevents value from growing beyond practical range.
  6. Serial output helps you verify each scaled value in sequence.

Real-life analogy

Imagine folding paper where thickness doubles each fold. That doubling behavior is exactly how *= 2 scales a value.

What Happens Inside

  1. Current variable value is loaded.
  2. Multiplication executes with right operand.
  3. Result is stored back into same variable.
  4. Next loop uses the scaled value.
  5. Repeated multiplication creates non-linear growth.

Common Mistakes with *=

  • Forgetting initialization before applying *=.
  • Using large factors that overflow quickly.
  • Missing max-bound reset in growing loops.
  • Using integer types without checking potential overflow limits.

Best Practices for *=

  • Use small multiplication factors for predictable behavior.
  • Add upper bounds and reset logic to control growth.
  • Log scaled values in Serial while tuning.
  • Keep multiplication factors small for controlled growth.
  • Always define upper bounds and reset conditions.

Practice Task

  1. Change factor from 2 to 3 and compare growth speed.
  2. Print blinkDelay before and after *=.
  3. Set max threshold to 500 instead of 800 and observe reset timing.
  4. Add a counter for total reset events in the loop.

Try it now

Open the simulator workspace and observe how *= scales values each loop cycle.

Run in Simulator