Magma's statements while and repeat perform iteration controlled by a Boolean condition.
Another control statement, for , iterates over each element of some enumerable domain.
Any number of statements may be in the body of these three statements.
In addition, the set and sequence constructors, and related constructors such as exists and forall , also iterate over a domain. They allow certain simple kinds of looping to be programmed in a static fashion.
// a run of the (3n+1) problem > x := Random(1, 100); > while x gt 1 do while> print x; while> if IsEven(x) then while|if> x div:= 2; while|if> else while|if> x := 3*x+1; while|if> end if; while> end while; 13 40 20 10 5 16 8 4 2> for g in Sym(3) do for> print g; for> end for; Id($) (1, 2, 3) (1, 3, 2) (2, 3) (1, 2) (1, 3)
> print forall(u){ m : m in [5..1000 by 4] | > exists{ <x, y, z> : x, y in [0..30], z in [-1, 0, 1] | > x^2+y^2+z eq m } }; false > print u; 77