Decrement Operator in C++

Level 240 keystrokes

Decreases x by 1 twice, once with post-decrement (x--) and once with pre-decrement (--x), so 10 becomes 8 and 8 is printed.

C++ code

// Initializes x with 10
1int x = 10;
// Post-decrement: x becomes 9 (its old value 10 is not used here)
2x--;
// Pre-decrement: x becomes 8; as a standalone statement both forms do the same
3--x;
// Prints the final value, 8
4cout << x << endl;

Output

8
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • -× 4
  • ;× 4
  • <× 4
  • =× 1

More C++ examples at level 2

← C++ typing practice