Easy Math

Since a computer does math for you, programming math is easy. All you have to know is which symbols to use.

The easiest form of math in programming takes this form :

result = number1 (operator) number2

An operator is the symbol that defines the mathematical operation, such as the plus sign.


The symbol (operator) for addition is the plus sign ( + ).

The total difficulty rating of two monsters.

total_difficulty = monst_1_diff + monst_2_diff

The player has just killed a monster in an RPG.

Give the player 2 more experience points.

experience = experience + 2;

This may look tricky at first. To help clarify, here are the steps that the computer takes to perform this calculation :

First, the experience variable on the right of the assignment operator is turned into the number it contains. We'll assume it contains the number 5.

experience = 5 + 2;

Then, it adds 5 to 2

experience = 7;

And finally, we are back to simply giving a variable a number to hold. This final step is just like one you learned in the last chapter.


The operator for subtraction is the minus sign ( - ).

The player has just been struck by a bullet.

Subtract 5 from the player's health.

player_health = player_health - 5


The operator for multiplication is ( * ). This is found above the 8 on your keyboard.

The final score equals the amount of enemies hit multiplied by 100.

final_score = enemies_hit * 100


The operator for division is ( / ). This is found next to the right shift key on your standard keyboard.

Given a certain amount of experience points, each member of a 4-member 
party will receive 1/4 of the total experience.

party_member_exp = total_exp_points / 4
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.