Using the ``every'' construct in Icon |
|
The general form of the ``every'' loop in Icon is:
every e1 do e2
This means, every time expression e1 produces a result,
evaluate expression e2.
The ``do e2'' part is optional.
The expression ``every e1'' means: compute every result
of e1 in sequence, discarding each one.
We can rewrite the simple arithmetic program this way:
procedure main()
local sum # Declare a variable for the sum
sum := 0; # Set the sum to zero
every sum +:= 1 to 5;
write ( "The sum of all numbers from 1 to 5 is ", sum );
end
The construct ``v +:= e'' means:
compute the value of expression e and add it to the
current value of variable v.