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

  1. Read the current value.
  2. Subtract 1 from that value.
  3. Store result back to the same variable.

Example: if countdown is 8, then countdown-- updates it to 7.

Pre-Decrement vs Post-Decrement

  • --x decrements 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
ExpressionReturned valueFinal variable
x--Old valueDecremented
--xNew valueDecremented

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-- and x -= 1 both reduce by one.
  • -- is shorter for simple counters.
  • -= 1 may 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

  1. postValue = countdown-- stores old value, then decrements.
  2. preValue = --countdown decrements first, then stores new value.
  3. LED blink marks each loop cycle visually.
  4. When countdown reaches 0, reset logic starts next cycle from 8.
  5. Serial logs show the difference between post and pre forms.

What Happens Inside

  1. CPU reads variable from memory.
  2. Arithmetic unit subtracts 1.
  3. Result is written back to memory.
  4. Expression returns old/new value based on post/pre form.
StepStatementValue usedcountdown after
1postValue = countdown--Old value-1
2preValue = --countdownNew value-1 again

Common Mistakes with --

  • Confusing x-- and --x in assignments.
  • Decrementing wrong counter in loop conditions.
  • Forgetting lower bounds and going below intended range.

Best Practices for --

  • Use clear names like countdown and retriesLeft.
  • 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

  1. Change initial countdown and observe post/pre values in Serial.
  2. Reset at a different threshold (for example 2) and compare loop behavior.
  3. Replace one decrement with -= 1 and compare output.
  4. Create a reverse for loop from 10 down to 1 using i--.

Try it now

Open the simulator workspace and observe variables while stepping through decrement examples.

Run in Simulator