Arithmetic Operators

Arithmetic Operators (1)

Arithmetic operators and their operands

Operator

Meaning

Operand type

*

multiplication

integer, floatingpoint

/

division

integer, floatingpoint

\%

modulo

integer

+

addition

integer, floatingpoint

-

subtraction

integer, floatingpoint

Attention

Integer division truncates!

Arithmetic Operators (2)

Precedence rules

  • Multiplication, division, modulo precede addition, subtraction

  • Operators with same precedence are associated from left to right

30 / 2 + 1

(30 / 2) + 1

precedence

1 + 30 / 2

1 + (30 / 2)

precedence

1 - 2 + 3

(1 - 2) + 3

left-associative

30 / 3 % 2

(30 / 3) % 2

left-associative