C/C++ Arena

Step 3 of 6

Changing a variable

A variable can be given a new value with =. The old value is replaced.

int hp = 100;
hp = hp - 27;     // now 73

Read hp = hp - 27 as: take the current value of hp, subtract 27, and store the result back in hp.

Your turn: the player has 3200 money and buys a rifle for 2700. Subtract the price from money so the program prints Money left: 500.

Previous: Printing values with %d Next: Decimals with double