for
 
 
 

Syntax:

for ( <initial statement>; <condition> ; <iteration statement>; ) <body statement>

Purpose:

The for statement provides a means of iterating loops of statements. The struc-ture of the for statement is considerably more powerful than that provided by most lan-guages. The particulars of it are different than ā€œCā€, but have a similar flavor.

The <initial statement> is any statement valid in the MODEL section. Typically it will be an assignment, but it is not limited to that. (It could be a (null) grouping or even a nested for statement.) An <initial statement> must be specified.

The <condition> is just an expression interpreted as a truth value. An expression is considered true if its value is non-zero, and false if its value is zero. If <condition> is true, then the <body statement> will be executed. If <condition> is false, then control immediately passes to the statement following the for state-ment. A <condition> must be specified.

The <iteration statement> is any statement valid in the MODEL section. An <iteration statement> must be specified. The <body statement> is any statement valid in the MODEL section. A <body statement> must be specified.

All the elements of a for statement are always required. The parentheses and semi-colon are part of the syntax, and required. The end of a for statement is marked by the end of the <body statement>.

There are four parts to the execution of a for statement. First, the <initial state-ment> is executed. Second, the <condition> is tested. The result of this is either continued execution or the end of the for statement. Third, if execution is to be continued, the <body statement> is executed. Fourth, the <iteration statement> is executed. Following this, control reverts to the second step, testing the <condition>, and pro-ceeds from that point. Execution continues in this way until the <condition> evalu-ates to false.

Example:

for ( x=0; x < 10; x = x+1;) print("digit =", x );