The Hyper Programming Language

'iterate' Statement

Warning: the 'iterate' statement has changed since the 0.4.3 compiler release; this page still documents the old syntax and semantics. The new syntax is described here.

General

The 'iterate' statement is designed for doing a certain number of iterations, and for iterating a collection. It is similar to the 'for' statement in Pascal and BASIC, but it is more general than the one in those languages.

Different forms

The 'iterate' statement can be used in different forms. For each form it is only the header that differs.

The simplest form just iterates a number of times. You specify an expression (called number here) that returns a natural number, to indicate the number of iterations that need to be done.

iterate count number
  statements
end iterate

Note that for each variant of iterate you are allowed to write just end instead of the full end iterate if you like.

The next form iterates with a variable that increments its value each iteration. The variable is declared in the iterate header and it can only be used inside the body of the iterate statement. The variable is initialized by the start-value and each iteration it is incremented (by using the variable's increment operator) until the variable equals the stop-value.

iterate variable : type from start-value to stop-value
  statements
end iterate

A similar form uses an iteration count instead of a stop-value. The iterate stops when the specified number of iterations has been done.

iterate variable : type from start-value count number
  statements
end iterate

As said before, by default the variable is simply incremented each iteration. This default behaviour can be explicitly expressed by adding step ++. But it is also possible to decrement the variable instead by using step --. Or, you can specify an expression that has to be added to the variable by using step increment-value. Some examples:

iterate variable : type from start-value to stop-value step --
  statements
end iterate
iterate variable : type from start-value count number step increment-value
  statements
end iterate

Nested 'iterate' statements can be joined together if the body of the outer 'iterate' only contains the inner 'iterate' :

iterate variable1 : type1 from start-value1 to stop-value1, variable2 : type2 from start-value2 to stop-value2
  statements
end iterate

Requirements

As 'iterate' should be usable for all class types (not only the built-in types), a number of requirements should be posed on the variable type, the start-value, the stop-value, the increment-value and the count number.

For the count number you can only use a value of one of the built-in natural number types: byte, nat, nat16, nat32 or nat64. Of course you can also use an expression of another type if that type is implicitly convertable to one of these natural number types.

For all forms except the simplest iterate count number an iterate variable and a step increment-value are involved. If a step was not specified, then the default step ++ is used.

The following tables list the different conditions together with the operators or constructors that are required (VAR = type of the variable, START = type of start-value, STOP = type of stop-value, STEP = type of increment-value if step is not ++ or --):

iterate variable
VAR.new(START)
step increment-value
expression ++ --
VAR += STEP
START <= STOP
VAR <= STOP
STOP <= VAR
VAR++
VAR = STOP (equality)
VAR--
VAR = STOP (equality)

Examples

The first example is a procedure that calculates the n-th Fibonnacci number (1, 1, 2, 3, 5, 8, 13, 21, ...):

procedure fib(n : nat) : nat
  var a & b : nat = 1
  
  iterate i : nat from 3 to n
    var temp : nat = a + b
    a = b
    b = temp
  end
  
  return b
end

This procedure calculates n! (faculty of n):

procedure fac(n : nat) : nat
  var f : nat = 1
  
  iterate i : nat from 2 to n
    f *= i
  end
  
  return f
end

The following procedure prints out the first n positive multiples of 3 (so n = 4 would print the numbers 0, 3, 6 and 9):

procedure print3(n : nat)
  iterate i : nat from 0 count n step 3
    system.out.printLn(i)
  end
end

The next procedure fills a two-dimensional array with the sum of the coordinates for each element:

procedure fillArray(a : * [ , ] nat)
  iterate x : nat from 0 count a.length(0), y : nat from 0 count a.length(1)
    a[x, y] = x + y
  end
end

See also


Valid HTML 4.01 Strict Valid CSS!