Operators: ++
Pre-increment and post-increment operator.
Syntax
Pre-increment:
++integer‑operand
++float‑operand
++char‑operand
Post-increment:
integer‑operand++
float‑operand++
char‑operand++
Discussion
The ++ operator increments 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 increment is performed before or after the operand itself is used within an expression. Accordingly we distinguish between the pre-increment and post-increment operator.
In the pre-increment, the operand is first incremented and then used inside the expression. Depending on the target system (C or JavaScript), the increment 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-increment 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 = 11, b = 21, c = 32
In the post-increment, the operand is first used inside the expression and then incremented. Depending on the target system (C or JavaScript), the increment 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-increment 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 = 11, b = 21, 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, increments 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, increments the value and stores it back using the onset method.
If necessary, the value resulting from the increment is automatically truncated to fit in the operand. For example, a variable of type uint8 can store values in range 0 .. 255. Trying to increment such variable containing already the maximum possible value 255 will result in an overflow to 0:
var uint8 a = 255; a++; // a = 0
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 increment.