Operators
Lesson 53: Using -- Decrement Operator
Learn how -- decreases values by one, including pre/post decrement behavior and loop usage.
Progress indicator
Lesson 53 of 57
Learning Objectives
- Understand what the decrement operator (--) does.
- Use -- to reduce values by one in loops and countdowns.
- Compare pre-decrement and post-decrement behavior.
- Compare -- with -= 1 and choose readable style.
- Avoid common decrement mistakes in expressions.
- Predict countdown values step-by-step during loop execution.
Concept Explanation
What is the Decrement Operator (--)
The -- operator subtracts 1 from a variable.
Decrement Operator Syntax
count--;
--count;How -- Works
- Read the current value.
- Subtract 1 from that value.
- Store result back to the same variable.
Example: if countdown is 8, then countdown-- updates it to 7.
Pre-Decrement vs Post-Decrement
--xdecrements first, then returns updated value.x--returns old value first, then decrements.
int x = 5;
int a = x--; // a=5, x=4
int b = --x; // x=3, b=3| Expression | Returned value | Final variable |
|---|---|---|
x-- | Old value | Decremented |
--x | New value | Decremented |
Using -- in Loops
-- is useful for countdown loops and reverse indexing.
for (int i = 5; i > 0; i--) {
// i decreases by 1 each iteration
}-- vs -= 1 (Comparison)
x--andx -= 1both reduce by one.--is shorter for simple counters.-= 1may be clearer in teaching code.
Data Type Notes
--is used with numeric types (int, long, etc.).- Decrementing unsigned values can underflow and wrap unexpectedly.
- Use lower bounds and reset logic in embedded countdowns.
When to Use --
- Countdown timers.
- Retry counters.
- Reverse loops and index movement.
Example Code
This sketch shows both post-decrement and pre-decrement values in Serial output.
const int LED_PIN = 2;
int countdown = 8;
int postValue = 0;
int preValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
postValue = countdown--;
preValue = --countdown;
digitalWrite(LED_PIN, HIGH);
delay(180);
digitalWrite(LED_PIN, LOW);
delay(180);
if (countdown <= 0) {
countdown = 8;
}
Serial.print("postValue=");
Serial.print(postValue);
Serial.print(", preValue=");
Serial.print(preValue);
Serial.print(", countdown=");
Serial.println(countdown);
}Example Code Explanation
postValue = countdown--stores old value, then decrements.preValue = --countdowndecrements first, then stores new value.- LED blink marks each loop cycle visually.
- When countdown reaches 0, reset logic starts next cycle from 8.
- Serial logs show the difference between post and pre forms.
What Happens Inside
- CPU reads variable from memory.
- Arithmetic unit subtracts 1.
- Result is written back to memory.
- Expression returns old/new value based on post/pre form.
| Step | Statement | Value used | countdown after |
|---|---|---|---|
| 1 | postValue = countdown-- | Old value | -1 |
| 2 | preValue = --countdown | New value | -1 again |
Common Mistakes with --
- Confusing
x--and--xin assignments. - Decrementing wrong counter in loop conditions.
- Forgetting lower bounds and going below intended range.
Best Practices for --
- Use clear names like
countdownandretriesLeft. - Keep decrement logic simple and readable.
- Add reset/limit checks for stable long-running loops.
- Print values while learning to verify post vs pre decrement behavior.
Practice Task
- Change initial
countdownand observe post/pre values in Serial. - Reset at a different threshold (for example 2) and compare loop behavior.
- Replace one decrement with
-= 1and compare output. - Create a reverse
forloop from 10 down to 1 usingi--.
Try it now
Open the simulator workspace and observe variables while stepping through decrement examples.