Increment and decrement expressions
AWK also supports the increment++
and decrement--
operators; they increase or decrease the value of a variable by one.Both operators are similar to the operators in C. These operators can only be used with a single variable and, thus, only before or after the variable.They are the short forms of some common operations of adding and subtracting.
Pre-increment:
It is represented by the plus plus (++
) symbolprefixed to the variable. It increments the value of an operand by one. Let's say we have a variable, var
; to pre-increment its value, we write ++var
.It first increments the value of operand and then returns the incremented value. In the following example, we use two variables,p =5
andq = ++p
.Here, first the value is incremented and then it is assigned. So,the pre-increment sets both the operands p
and q
to 6
.It is equivalent to p=p+1
and then q=p
, as follows:
$ vi pre-increment.awk BEGIN { p = 5; q = ++p; printf "p = %d, q = %d\n", p,q ...