z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Operation ordering

z/OS UNIX System Services User's Guide
SA23-2279-00

Expressions can contain several operations, as in:
A+B*C
As is customary in mathematics, all multiplications and divisions and remainder operations are performed before additions and subtractions. When handling the foregoing expression, awk performs B*C first and then adds A. The value of:
2+3*4
is therefore 14 (3*4 first, then add 2). If you want a particular operation done first, enclose it in parentheses, as in:
(A+B)*C
When evaluating this expression, awk performs the addition before the multiplication. Therefore:
(2+3)*4
is 20 (2+3 first, then multiply by 4). As an example of this, consider the program:
{ print $4/($3*52) }
$4 is the amount of money a person spent on a hobby in the last year. $3 is the average number of hours a week the person spent on that hobby, so $3*52 is the number of hours in 52 weeks (that is, 1 year). $4/($3*52) is therefore the amount of money that the person spent on the hobby per hour.

An order-of-operations table for awk can be found in the awk command description in z/OS UNIX System Services Command Reference.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014