A function body is a sequence of statements. So far all our statements were "void expressions" - an expression whose result is discarded.
This chapter shows all the other statements available. These statements change the flow of program execution: instead of sequentially executing statements they can selectively skip some statements or execute some statemets repeatedly.
The conditional statement is called if. The syntax is:
Multiline version (allows else):
if (cond) stmt1;
if (cond) stmt1; else stmt2;
if cond then stmt
if cond then
stmt
end if
if cond then
stmt1
else
stmt2
end if
if cond then stmt1;
if cond then stmt1 else stmt2;
where cond is an expression that is evaluated. If the result is
true, stmt1 is executed (else it is skipped). stmt1 is
a single statement (or a block, see chapter 1). If an else is
present,when cond is false, stmt2 is executed.
Syntax:
while (cond) stmt
do while cond
stmt
loop
while (cond) stmt
This will execute stmt repeatedly, 0 or more times, as long as
cond is true. For example the following script will print
1, 2 and 3:
It is a pre-test loop because cond is evaluated before executing
stmt - this can result in zero executions of stmt if
the condition is false before the first iteration.
When until is used instead of while, the condition is inverted:
the loop continues until the condition becomes true (so the loop is executed
as long as the condition is false).
Syntax:
This will execute stmt repeatedly, 1 or more times, as long as
cond is
It is a post-test loop because cond is evaluated after executing
stmt - this means stmt is always executed at least once,
even if the condition is
When until is used instead of while, the condition is inverted:
the loop continues until the condition becomes true (so the loop is executed
as long as the condition is false).
The for loop is a shorthand to the following, more verbose code:
which makes the for loop a pre-test loop. The following example prints
numbers 1, 2 and 3 (and is equivalent to the example in 3.3):
Note: unlike in C, there is no comma operator in fawk, so something
like n = 1, b = 2 as init will not work.
The for loop runs var from value expr1 to expr2
in expr3 steps. The "step expr3" part is optional, when
omitted +1 or -1 is used. next is always interpreted for the
closest for, specifying var is optional.
The following example prints numbers 1, 2 and 3 (and is equivalent
to the example in 3.3):
The following example prints numbers 5, 10, 15 and 20:
Note: the value of n after the loop is the last value it had in
the loop - normally expr2, where the loop has terminated.
The for loop runs var from value expr1 to expr2
in +1 increments or -1 decrements depending on whether to or
downto was used.
The following example prints numbers 1, 2 and 3 (and is equivalent
to the example in 3.3):
Note: the value of n after the loop is the last value it had in
the loop: expr2, where the loop has terminated.
3.4. post-test loop
do stmt while (cond)
do
stmt
loop while cond
repeat
stmt
stmt
until cond
3.5. for loop
for (init; cond; step) stmt
init
while (cond) {
stmt
step
}
function main(ARGV)
{
for(n = 1; n < 4; n++)
fawk_print(n);
}
for var = expr1 to expr2 step expr3
stmt
next var
for n = 1 to 3
fawk_print(n)
next n
for n = 5 to 20 step 5
fawk_print(n)
next n
for var := expr1 to expr2 do
stmt
or
for var := expr1 downto expr2 do
stmt
function main(ARGV);
begin
for n := 1 to 3 do
fawk_print(n);
end;