next up previous contents
Next: The while Statement Up: Iteration Previous: Iteration

The for Command

The syntax of the for command in Darwin is as follows

		for var {from bgn} { to finish } {byincr} do 
{ break }
{ next }
od

The brace symbols { and } denote optional code. If the from bgnis not included in the for command definition, the value 1is assumed as the starting point. If to finish is omitted from the declaration, the for loop does not terminate unless a break is encountered. If the by incr is not included in the command, the value 1 is assumed as the increment incr. Initially, the variable var is assigned the value bgn. The body of a for loop is executed if and only if the value assigned to variable varis less than or equal to finish. After the last statement of the body is executed, the variable var is assigned the value var + incr each time through the loop. If var remains less than or equal to finish, the body is executed once again.

> for i from 1 to 100 by 10 do
>   printf('%d ', i);
> od;
1 11 21 31 41 51 61 71 81 91
The built-in Darwin name break immediately ends execution of the for loop regardless of the stage of execution. Encountering a next command in the body causes the remaining statements (those following the next command to the end of the body) not to be executed. Control is sent back to the first line of the for loop where Darwin decides to execute the body if the incremented value in varis still less than or equal to higher.

The following loop never unravels in its entirety since a break command is encounter when i is assigned the value 3. Note that implicitly the for loop begins counting at 1.

> for i to 5 do             # equivalent to: for i from 1 to 5 do
>   print(i);
>   if (i=3) then break; fi;
> od;
1
2
3

The body of the following for loop is executed exactly four times in the following example.

> for i from 100.1 to 94 by -2 do
>   next;                                # no output is generated.
>   lprint(i);
> od;

It is possible to assign to the variable var within the body of the loop. However, this is considered poor programming style and can result in difficult to understand and debug programs.

> for i to 2 do
>   i := 1;                   # an assignment to the loop variable
> od;
<infinite loop>

There is an alternative second syntax for the for loop. It allows one to traverse through a list, array or set easily.


		for var in obj do 
{ break }
{ next }
od

Here obj must be either a set, list, or array.

> for i in {1, 2, 3, 17, 19, 20} do
>   printf('Amino Acid: %s\n', IntToA(i));
> od;
Amino Acid: A
Amino Acid: R
Amino Acid: N
Amino Acid: T
Amino Acid: Y
Amino Acid: V

> for j in ['A', 'C', 'G', 'T', 'U'] do
>   printf('Nucleic Acid: %s\n', (IntToNucleic(NToInt(j))));
> od;
Nucleic Acid: Adenine
Nucleic Acid: Cytosine
Nucleic Acid: Guanine
Nucleic Acid: Thymine
Nucleic Acid: Uracil


next up previous contents
Next: The while Statement Up: Iteration Previous: Iteration
Gaston Gonnet
1998-09-15