Operators: --

Pre-decrement and post-decrement operator.

Syntax

Pre-decrement:

--integer‑operand

--float‑operand

--char‑operand

Post-decrement:

integer‑operand--

float‑operand--

char‑operand--

Discussion

The -- operator decrements the value stored in a signed or unsigned integer, a floating point or char operand. It is an equivalent for the operation variable = variable - 1. Depending on the placement of the operator relative to the affected operand, the decrement is performed before or after the operand itself is used within an expression. Accordingly we distinguish between the pre-decrement and post-decrement operator.

In the pre-decrement, the operand is first decremented and then used inside the expression. Depending on the target system (ANSI C or JavaScript), the decrement can be performed either still before the evaluation of the entire expression has begun or just in the moment when the affected operand is about to be evaluated. The -- pre-decrement operator is placed on the left side of the operand it affects:

var int32 a = 10;
var int32 b = 20;

var int32 c = --a + --b;  // a = 9, b = 19, c = 28

In the post-decrement, the operand is first used inside the expression and then decremented. Depending on the target system (ANSI C or JavaScript), the decrement can be performed either after the evaluation of the entire expression was finished or just in the moment when the affected operand has been evaluated. The -- post-decrement operator is placed on the right side of the operand it affects:

var int32 a = 10;
var int32 b = 20;

var int32 c = a-- + b--;  // a = 9, b = 19, c = 30

If the operator is applied to a variable, array element, local variable, local array element or method parameter, the operation reads the content stored in the memory associated to the operand, decrements the value and writes the modified value back to memory. In case of a property as operand, the operator reads the value of the property using its onget method, decrements the value and stores it back using the onset method.

If necessary, the value resulting from the decrement is automatically truncated to fit in the operand. For example, a variable of type uint8 can store values in range 0 .. 255. Trying to decrement such variable containing already the minimum possible value 0 will result in an overflow to 255:

var uint8 a = 0;
a--;  // a = 255

Side effects

Chora compiler detects and reports operands with side effects. An operand with side effects is an operand evaluated and modified within the same expression. The result of this operation is not predictable and can depend on the order in which the particular target system executes the individual operations. For example, the operation a = a-- * a is considered as containing an operand with side effects because the variable a is modified and evaluated multiple times within the expression. Especially in the last occurrence of the variable it is not certain whether it will contain the value after or still before the decrement.