Mathematical Operations

Mathematical Operations

Here is how to do basic math – addition, subtraction, multiplication, division.

Addition:

health = health + 10;          //this adds 10 to the integer variable health and stores the answer
health += 10;                       //short cut – it means the same as health = health + 10
health ++;                            //this adds 1 to health  – same as health += 1
//or health = health + 1

Subtraction:

health = health – 10;          //this subtracts 10 from the integer variable health
health -= 10;                       //short cut – it means the same as health = health – 10
health –;                            //this add 1 to health – same as health -= 1
//or health = health – 1

Multiplication:

health = health * 2;          //this multiplies health by 2
health *= 2;                       //short cut – it means the same as health = health * 2

Division:

health = health / 2;          //this divides health by 2
health /= 2;                       //short cut – it means the same as health = health / 2

The computer follows the order of operations.