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
- Read current variable value.
- Multiply by right-side operand.
- Store result back in the same variable.
Updating Variables with *=
value *= 2doubles value each cycle.pwm *= 2quickly scales brightness steps.delayMs *= factorincreases timing exponentially.
*= vs * and = (Comparison)
x = x * 3andx *= 3do the same update.*=is shorter and clearer in repeated loops.
x = x * 3; // long form
x *= 3; // compound formUsing *= in Loops
Use *= when values need to grow by scale factors each loop.
Real embedded examples
sampleWindow *= 2for adaptive timing intervals.gain *= 2while increasing amplifier sensitivity in steps.pwmStep *= factorfor 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.
| Cycle | blinkDelay | Update |
|---|---|---|
| Start | 100 | Initial value |
| After loop 1 | 200 | 100 *= 2 |
| After loop 2 | 400 | 200 *= 2 |
| After loop 3 | 800 | 400 *= 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
blinkDelaystarts at 100 ms and controls blink timing.cycleCount += 1increments each loop for progress tracking.blinkDelay *= factormultiplies delay by 2 on every cycle.- This creates fast growth: 100, 200, 400, 800, ...
- Reset logic prevents value from growing beyond practical range.
- 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
- Current variable value is loaded.
- Multiplication executes with right operand.
- Result is stored back into same variable.
- Next loop uses the scaled value.
- 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
- Change
factorfrom 2 to 3 and compare growth speed. - Print
blinkDelaybefore and after*=. - Set max threshold to 500 instead of 800 and observe reset timing.
- 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.