Language Elements and Reference:xpplrm

Increment and decrement operators Foundation

Increment and decrement operators are unary operators whose operands must be of "numeric" or "date" data type. The operators increase or decrease the value of their operands by 1.

Increment and decrement operators
Operator Data type Operation
++ N, D Increase value by 1 and assign
-- N, D Decrease value by 1 and assign

Both of these operators can be used as either prefix or postfix notation. This has significance in complex expressions. Prefix notation first changes the value of the operand and then evaluates the remainder of the expression. Postfix notation, on the other hand, first evaluates the expression and then changes the value of the operand. The following program code illustrates this positional dependence in an example of the increment operator:

nValue  := 10 
nResult := ++nValue * 3          // ** Prefix notation ** 
? nResult                        // result: 33 
? nValue                         // result: 11 

nValue  := 10 
nResult := nValue++ * 3          // ** Postfix notation ** 
? nResult                        // result: 30 
? nValue                         // result: 11 

Using prefix notation, the value nValue is first incremented, then multiplication is performed. Using postfix notation, multiplication is performed first and then the value nValue is incremented. The results of the two operations is different as shown in nResult.

The following program code shows the same example using the decrement operator. The result here is also dependent on the operator position:

nValue  := 10 
nResult := --nValue * 3          // ** Prefix-Notation ** 
? nResult                        // result: 27 
? nValue                         // result:  9 

nValue  := 10 
nResult := nValue-- * 3          // ** Postfix notation ** 
? nResult                        // result: 30 
? nValue                         // result:  9 

When either of the two operators is used with an operand having the "date" data type, the date value increases or decreases by one calendar day.

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.