9.0 Controlling Program Flow
9.1. Directing Script Execution
Control flow statements are essential for creating non-trivial programs that can make decisions and perform repetitive tasks. This section covers AWK’s conditional statements (if) for decision-making and its looping constructs (for, while) for iteration.
9.2. Conditional Statements
The Statement
The if statement executes an action only if a specified condition is true.
- Syntax: if (condition) action
- Block Syntax: if (condition) { action-1; action-2; … }
This example checks if a number is even:
[jerry]$ awk ‘BEGIN {num = 10; if (num % 2 == 0) printf “%d is even number.\n”, num }’
# Output:
# 10 is even number.
The Statement
The if-else statement provides an alternative action to execute when the condition is false.
- Syntax: if (condition) action-1 else action-2
This example checks if a number is even or odd:
[jerry]$ awk ‘BEGIN { num = 11; if (num % 2 == 0) printf “%d is even number.\n”, num; else printf “%d is odd number.\n”, num }’
# Output:
# 11 is odd number.
The Ladder
Multiple conditions can be chained together to test for various possibilities.
[jerry]$ awk ‘BEGIN { a = 30; if (a==10) print “a = 10”; else if (a == 20) print “a = 20”; else if (a == 30) print “a = 30”; }’
# Output:
# a = 30
9.3. Looping Constructs
The Loop
The for loop provides a classic structure for iteration, consisting of an initializer, a condition, and an incrementor.
- Syntax: for (initialization; condition; increment/decrement) action
This example prints the numbers from 1 to 5:
[jerry]$ awk ‘BEGIN { for (i = 1; i <= 5; ++i) print i }’
# Output:
# 1
# 2
# 3
# 4
# 5
The Loop
The while loop repeats an action as long as its condition remains true. The condition is tested at the beginning of each iteration.
- Syntax: while (condition) action
[jerry]$ awk ‘BEGIN {i = 1; while (i < 6) { print i; ++i } }’
# Output:
# 1
# 2
# 3
# 4
# 5
The Loop
The do-while loop is similar, but it tests the condition at the end of the loop. This guarantees the loop body will execute at least once.
- Syntax: do action while (condition)
[jerry]$ awk ‘BEGIN {i = 1; do { print i; ++i } while (i < 6) }’
# Output:
# 1
# 2
# 3
# 4
# 5
9.4. Loop Control Statements
The Statement
The break statement immediately terminates the innermost for, while, or do-while loop it is in.
[jerry]$ awk ‘BEGIN { sum = 0; for (i = 0; i < 20; ++i) { sum += i; if (sum > 50) break; else print “Sum =”, sum } }’
# Output:
# Sum = 0
# Sum = 1
# Sum = 3
# Sum = 6
# Sum = 10
# Sum = 15
# Sum = 21
# Sum = 28
# Sum = 36
# Sum = 45
The Statement
The continue statement skips the remainder of the current loop iteration and proceeds to the next one. This example prints only even numbers.
[jerry]$ awk ‘BEGIN { for (i = 1; i <= 20; ++i) { if (i % 2 == 0) print i ; else continue } }’
# Output:
# 2
# 4
# 6
# 8
# 10
# 12
# 14
# 16
# 18
# 20
The Statement
The exit statement terminates the entire AWK script immediately, not just a loop. It can optionally return a status code to the shell.
[jerry]$ awk ‘BEGIN { sum = 0; for (i = 0; i < 20; ++i) { sum += i; if (sum > 50) exit(10); else print “Sum =”, sum } }’
After the script exits, you can check its status code:
[jerry]$ echo $?
# Output:
# 10
As programs grow in complexity, it becomes necessary to organize code into reusable blocks, which is the purpose of functions.