(gawk) Conditional Exp
Info Catalog
(gawk) Boolean Ops
(gawk) Expressions
(gawk) Function Calls
Conditional Expressions
=======================
A "conditional expression" is a special kind of expression with
three operands. It allows you to use one expression's value to select
one of two other expressions.
The conditional expression is the same as in the C language:
SELECTOR ? IF-TRUE-EXP : IF-FALSE-EXP
There are three subexpressions. The first, SELECTOR, is always
computed first. If it is "true" (not zero and not null) then
IF-TRUE-EXP is computed next and its value becomes the value of the
whole expression. Otherwise, IF-FALSE-EXP is computed next and its
value becomes the value of the whole expression.
For example, this expression produces the absolute value of `x':
x > 0 ? x : -x
Each time the conditional expression is computed, exactly one of
IF-TRUE-EXP and IF-FALSE-EXP is used; the other is ignored. This is
important when the expressions have side effects. For example, this
conditional expression examines element `i' of either array `a' or
array `b', and increments `i'.
x == y ? a[i++] : b[i++]
This is guaranteed to increment `i' exactly once, because each time
only one of the two increment expressions is executed, and the other is
not. Arrays in `awk' Arrays, for more information about arrays.
As a minor `gawk' extension, you can continue a statement that uses
`?:' simply by putting a newline after either character. However, you
cannot put a newline in front of either character without using
DONTPRINTYET backslash continuation ( `awk' Statements Versus Lines
Statements/Lines.). If `--posix' is specified (*note Command Line
DONTPRINTYET backslash continuation ( `awk' Statements Versus Lines
Statements/Lines.). If `--posix' is specified ( Command Line
Options Options.), then this extension is disabled.
Info Catalog
(gawk) Boolean Ops
(gawk) Expressions
(gawk) Function Calls
automatically generated byinfo2html