Next: , Previous: Assignment Ops, Up: Expressions



5.8 Increment and Decrement Operators

Increment and decrement operators increase or decrease the value of a variable by one. An assignment operator can do the same thing, so the increment operators add no power to the awk language; however, they are convenient abbreviations for very common operations.

The operator used for adding one is written `++'. It can be used to increment a variable either before or after taking its value. To pre-increment a variable v, write `++v'. This adds one to the value of v—that new value is also the value of the expression. (The assignment expression `v += 1' is completely equivalent.) Writing the `++' after the variable specifies post-increment. This increments the variable value just the same; the difference is that the value of the increment expression itself is the variable's old value. Thus, if foo has the value four, then the expression `foo++' has the value four, but it changes the value of foo to five. In other words, the operator returns the old value of the variable, but with the side effect of incrementing it.

The post-increment `foo++' is nearly the same as writing `(foo += 1) - 1'. It is not perfectly equivalent because all numbers in awk are floating-point—in floating-point, `foo + 1 - 1' does not necessarily equal foo. But the difference is minute as long as you stick to numbers that are fairly small (less than 10e12).

Fields and array elements are incremented just like variables. (Use `$(i++)' when you want to do a field reference and a variable increment at the same time. The parentheses are necessary because of the precedence of the field reference operator `$'.)

The decrement operator `--' works just like `++', except that it subtracts one instead of adding it. As with `++', it can be used before the lvalue to pre-decrement or after it to post-decrement. Following is a summary of increment and decrement expressions:

++lvalue
This expression increments lvalue, and the new value becomes the value of the expression.
lvalue++
This expression increments lvalue, but the value of the expression is the old value of lvalue.


--lvalue
This expression is like `++lvalue', but instead of adding, it subtracts. It decrements lvalue and delivers the value that is the result.
lvalue--
This expression is like `lvalue++', but instead of adding, it subtracts. It decrements lvalue. The value of the expression is the old value of lvalue.

Advanced Notes: Operator Evaluation Order

Doctor, doctor! It hurts when I do this!
So don't do that!

Groucho Marx

What happens for something like the following?

     b = 6
     print b += b++

Or something even stranger?

     b = 6
     b += ++b + b++
     print b

In other words, when do the various side effects prescribed by the postfix operators (`b++') take effect? When side effects happen is implementation defined. In other words, it is up to the particular version of awk. The result for the first example may be 12 or 13, and for the second, it may be 22 or 23.

In short, doing things like this is not recommended and definitely not anything that you can rely upon for portability. You should avoid such things in your own programs.