gawk: If Statement
1
1 7.4.1 The 'if'-'else' Statement
1 -------------------------------
1
1 The 'if'-'else' statement is 'awk''s decision-making statement. It
1 looks like this:
1
1 'if (CONDITION) THEN-BODY' ['else ELSE-BODY']
1
1 The CONDITION is an expression that controls what the rest of the
1 statement does. If the CONDITION is true, THEN-BODY is executed;
1 otherwise, ELSE-BODY is executed. The 'else' part of the statement is
1 optional. The condition is considered false if its value is zero or the
1 null string; otherwise, the condition is true. Refer to the following:
1
1 if (x % 2 == 0)
1 print "x is even"
1 else
1 print "x is odd"
1
1 In this example, if the expression 'x % 2 == 0' is true (i.e., if the
1 value of 'x' is evenly divisible by two), then the first 'print'
1 statement is executed; otherwise, the second 'print' statement is
1 executed. If the 'else' keyword appears on the same line as THEN-BODY
1 and THEN-BODY is not a compound statement (i.e., not surrounded by
1 braces), then a semicolon must separate THEN-BODY from the 'else'. To
1 illustrate this, the previous example can be rewritten as:
1
1 if (x % 2 == 0) print "x is even"; else
1 print "x is odd"
1
1 If the ';' is left out, 'awk' can't interpret the statement and it
1 produces a syntax error. Don't actually write programs this way,
1 because a human reader might fail to see the 'else' if it is not the
1 first thing on its line.
1