Go forward to While Statement.
Go backward to Statements.
Go up to Statements.
The `if' Statement
==================
The `if'-`else' statement is `awk''s decision-making statement. It
looks like this:
if (CONDITION) THEN-BODY [else ELSE-BODY]
CONDITION is an expression that controls what the rest of the statement
will do. If CONDITION is true, THEN-BODY is executed; otherwise,
ELSE-BODY is executed (assuming that the `else' clause is present).
The `else' part of the statement is optional. The condition is
considered false if its value is zero or the null string, and true
otherwise.
Here is an example:
if (x % 2 == 0)
print "x is even"
else
print "x is odd"
In this example, if the expression `x % 2 == 0' is true (that is,
the value of `x' is divisible by 2), then the first `print' statement
is executed, otherwise the second `print' statement is performed.
If the `else' appears on the same line as THEN-BODY, and THEN-BODY
is not a compound statement (i.e., not surrounded by curly braces),
then a semicolon must separate THEN-BODY from `else'. To illustrate
this, let's rewrite the previous example:
awk '{ if (x % 2 == 0) print "x is even"; else
print "x is odd" }'
If you forget the `;', `awk' won't be able to parse the statement, and
you will get a syntax error.
We would not actually write this example this way, because a human
reader might fail to see the `else' if it were not the first thing on
its line.