HP C A.06.05 Reference Manual

Statements
for
Chapter 6 161
for
Syntax
for ([
expression1
]; [
expression2
]; [
expression3
])
statement;
Arguments
expression1
This is the initialization expression that typically specifies the initial values
of variables. It is evaluated only once before the first iteration of the loop.
expression2
This is the controlling expression that determines whether or not to
terminate the loop. It is evaluated before each iteration of the loop. If
expression2
evaluates to a nonzero value, the loop body is executed. If it
evaluates to 0, execution of the loop body is terminated and control passes to
the first statement after the loop body. This means that if the initial value of
expression2
evaluates to zero, the loop body is never executed.
expression3
This is the increment expression that typically increments the variables
initialized in
expression1
. It is evaluated after each iteration of the loop
body and before the next evaluation of the controlling expression.
Description
The for statement executes the statements within a loop as long as
expression2
is true. The
for statement is a general-purpose looping construct that allows you to specify the
initialization, termination, and increment of the loop. The for uses three expressions.
Semicolons separate the expressions. Each expression is optional, but you must include the
semicolons.
How the for Loop is Executed
The for statement works as follows:
1. First,
expression1
is evaluated. This is usually an assignment expression that initializes
one or more variables.
2. Then
expression2
is evaluated. This is the conditional part of the statement.
3. If
expression2
is false, program control exits the for statement and flows to the next
statement in the program. If
expression2
is true,
statement
is executed.