Compound Assignment in C++

Level 246 keystrokes

Changes x with the compound assignment operators += and *=: 10 becomes 15, then 30, and 30 is printed.

C++ code

// Initializes x with 10
1int x = 10;
// x += 5 is short for x = x + 5, so x becomes 15
2x += 5;
// x *= 2 is short for x = x * 2, so x becomes 30
3x *= 2;
// Prints the final value, 30
4cout << x << endl;

Output

30
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
  • =× 3
  • *× 1
  • +× 1

More C++ examples at level 2

← C++ typing practice