News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


How expressions work in Icon

There are two important properties of Icon expressions:

  1. Icon has no statements, just expressions.
  2. In general, an expression is a sequence of operations that may produce zero or more results.

In general, Icon loops take the form ``every e1 do e2'' where e1 and e2 can be any expression. If e1 produces no results, e2 is never evaluated. If it produces 19 results, e2 is evaluated 19 times, and so on.

Let's take another look at the loop in the simple arithmetic program:

      every  n := 1 to 5  do   # For n equal to 1, 2, 3, 4, 5 ...
        sum  :=  sum + n;      # ...add n to the sum
In this case, the expression ``n:=1 to 5'' produces five results, so the loop is executed five times. The expression ``1 to 5'' produces the values from 1 to 5 in sequence.
Next: Using the ``every'' construct in Icon
See also: Using the Icon programming language
Previous: Doing simple arithmetic in Icon

John W. Shipman, john@nmt.edu