next up previous contents
Next: Creating arrays of instances Up: Constructor loops Previous: Constructor loops   Contents

Basic for-loops

For example,

        for(i = 0; i < 3; i = i + 1){
          x[i] := i;
        }
is in every way equivalent to

        x[0] := 0;
        x[1] := 1;
        x[2] := 2;
The general form of the loop is

        for(var = init; cond; var = next)
          <stmt>

The loop is unrolled in the following way: initially, var is set to init. Then, while cond is true, stmt is instantiated, and var is set to next. The loop variable var may appear in stmt. Each occurrence of var is replaced by its current value.



2002-10-28