nudge operator

The *nudge operators* can be used to [[increment?]] or [[decrement?]] the value of their [[operand?]]s, depending on which operator is used. The *increment operator* (nudge up), is represented by a [[plusplus?]] symbol and the *decrement operator* (nudge down), is represented by a [[doubledash?]] symbol. The nudge operators modify their [[operand?]]s, so the [[operand?]]s must be [[variable_name?]]s:

 number++    # increment
 number--    # decrement

Nudge operators can exhibit pre-nudge or post-nudge behaviour

The nudge operators can exhibit pre-nudge or post-nudge behaviour, depending on whether they are placed to the left or to the right of their [[operand?]]s.

prenudge behaviour

If the operator is used before a variable name, the operator exhibits pre-nudge behaviour and the variable is changed, before the [[expression?]] is evaluated.

# preincrement
number = 5
result = ++number    # result is 6 and number is 6

# predecrement
number = 5
result = --number    # result is 4 and number is 4

postnudge behaviour

If the operator is used after the variable name, the operator exhibits post-nudge behaviour and the variable is changed after the [[expression?]] has been evaluated:

# postincrement
number = 5
result = number++    # result is 5 but number is 6

# postdecrement
number = 5
result = number--    # result is 5 but number is 4

Nudging floating point values

When used against floating point values, the nudge operators will attempt to add or subtract one to obtain a return value:

number = 5.4
result = ++number    # result is 6.4
number = 0.4
result = --number    # result is -0.6

Nudging strings

Numerical strings will evaluated as traditional numerical values

In awk, numerical strings will evaluated as normal numeric values within an expression, so the nudge operators will return appropriate results:

number = "1"
result = ++number    # result is 2
number = "0"
result = --number    # result is 1
number = "-0.4"
result = --number    # result is -1.4

Non numerical strings will be nudged as a zero

Non numerical strings will evaluated as zero within a numeric expression, and the nudge operators will return a value relative to that:

string = "abc"
result = ++string    # 1
string = "abc"
result = --string    # -1