Pre/Post Increment in C++

Level 252 keystrokes

Shows the difference between post-increment and pre-increment: a++ prints the old value 5, then ++a raises a to 7 and prints 7.

C++ code

// Initializes a with 5
1int a = 5;
// Post-increment: the expression gives the old value, so 5 is printed
// a is increased afterwards and becomes 6
2cout << a++ << endl;
// Pre-increment: a goes from 6 to 7 first, then 7 is printed
3cout << ++a << endl;

Output

5
7
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.

  • <× 8
  • +× 4
  • ;× 3
  • =× 1

More C++ examples at level 2

← C++ typing practice