Arithmetic

Resources:

Arithmetic

Syntax Introduced:

The expression to the right of the = symbol is evaluated before the value is assigned to the variable on the left. Therefore, the statement a=5+4 first adds 5 and 4 to yield 9 and then assigns the value 9 to the variable a.

Parentheses are used to enforce order of operations:

The following exampl shows how to use variables in the arguments (parameters) of functions. Note that a calculation can be sent as an argument as well. Try modifying the example to draw horizontal lines.

The Modulus Operator

The modulus operator (%) in the format A % B, returns the remainder from dividing A by B. It's useful for creating patterns or cyclical events.

Expression Result Explanation
9 % 3 0 3 goes into 9 three times, with no remainder
9 % 2 1 2 goes into 9 four times, with 1 as the remainder
35 % 4 3 4 goes into 35 eight times, with 3 as the remainder

Arithmetic Shortcuts

The ++ operator increments a variable by 1:

The -- operator decrements a variable by 1:

The += operator increments a variable by another number:

The -= operator decrements a variable by another number:

Arithmetic Rounding

round()
ceil()
floor()
min()
max()