Iteration Statement

Scheme: (dotimes ( i n ) code )
Pascal: for i := 0 to n - 1 do code
C++, Java: for ( i = 0; i < n ; i ++) code

In the dotimes form, the index always starts at 0 and ranges through end - 1. In Pascal, the starting index can be specified, and it ranges through end. The for statement in C++ and Java specifies an initialization, test, and increment, similar to the do of Lisp.

If multiple statements are to be executed within the loop, a begin ... end or { ... } must be used.


{  int i; float max, sum; float values[10];
   max = values[0];
   sum = 0.0;
   for (i = 0; i < 10; i++)
     {
       if (values[i] > max)
           max = values[i];
       sum = sum + values[i];
     }
}

Contents    Page-10    Prev    Next    Page+10    Index