Expressions and operators

An expression is an arithmetical operation that delivers a result, which can then be assigned to a variable or object parameter. The arithmetic expression may be composed of any numbers, further variables or object parameters, function calls, brackets, and arithmetic operators.

The following operators are available in expressions:
= Assigns the result right of the '=' to the variable left of the '='.
+-*/ The usual mathematical operators. * and / have a higher priority than + and -.
% Modulo operator, the remainder of an integer division (see also fmod).
| Bitwise OR, can be used to set certains bits in an integer variable.
^ Bitwise exclusive OR, can be used to toggle certain bits in an integer variable.
~ Bitwise invert, toggles all bits of an integer variable.
& Bitwise AND, can be used to reset certains bits in an integer variable.
>> Bitwise right shift, can be used to divide a positive integer value by 2.
<< Bitwise left shift, can be used to multiply a positive integer value by 2.
() Brackets, for defining the priority of mathematical operations. Always use brackets when priority matters!

Examples:

x = (a + 1) * b / c;
z = 10;
x = x >> 2; // divides x by 4 (integer only)
x = x << 3; // multiplies x by 8  (integer only)
x = fraction(x) << 10; // copies the fractional part of x (10 bits) into the integer part

Assignment operators

The "="-character can be combined with the basic operators:

+= Adds the result right of the operator to the variable left of the operator.
-= Subtracts the result right of the operator from the variable left of the operator.
*= Multiplies the variable left of the operator by the result right of the operator.
/= Divides the variable left of the operator by the result right of the operator.
%= Sets the variable left of the operator to the remainder of the integer division by the result right of the operator.
|= Bitwise OR of the result right of the operator with the variable left of the operator.
&= Bitwise AND of the result right of the operator with the variable left of the operator.
^= Bitwise exclusive OR of the result right of the operator and the variable left of the operator.
>>= Bitwise right shift the variable left of the operator by the result right of the operator.
<<= Bitwise left shift the variable left of the operator by the result right of the operator.

Increment and decrement operators

By placing a '++' at the end of a variable, 1 is added; by placing a '--', 1 is subtracted. This is a convenient shortcut for counting a variable up or down.

Examples:

x = x + 1; // add 1 to x
z += 1; // add 1 to x
x++; // add 1 to x (integer only)

Remarks:

See also:

Functions, Variables, Pointers, Comparisons

 

► latest version online