   ### 10.3.3  The for loop

The for loop has three different forms, each of which uses an index variable. If the for loop is used in a program, the index variable should be declared as a local variable. (Recall that i represents the imaginary unit, and so cannot be used as the index.)

##### The first form

For the first form, the for is followed by the starting value for the index, the end condition, and the increment step, separated by semicolons and in parentheses. Afterwards is a block of code to be executed for each iteration. For example, to add the even numbers less than 100, you can set the running total to 0,

S := 0

and the use an for loop to do the summing,

for (j := 0; j < 100; j := j + 2) {S := S + j}
##### The second form

The second form of a for loop has a fixed increment for the index. It is written out with for followed by the index, followed by from, the initial value, to, the ending value, step, the size of the increment, and finally the statements to be executed between do and end_for. For example, having set the variable S equal to 0, you can again add the even numbers less than 100 with

for j from 2 to 98 step 2 do S := S + j; end_for

There is also a French version of this syntax;

pour j de 2 jusque 98 pas 2 faire S := S + j; fpour
##### The third form

The third form of the for loop lets you iterate over the values in a list (or a set or a range). In this form, the for is followed by the index, then in, the list, and then the instructions between do and end_for. For example, to add all integers from 1 to 100, you can again set the running total S to 0, then

for j in 1..100 do S:= S + j; end_for

or

pour j in 1..100 faire S:= S + j; fpour   