4.19 For

  • for simple-var in list-expr do statements od;

    The for loop executes the statement sequence statements for every element of the list list-expr.

    The statement sequence statements is first executed with simple-var bound to the first element of the list list, then with simple-var bound to the second element of list and so on. simple-var must be a simple variable, it must not be a list element selection list-var[int-expr] or a record component selection record-var.ident.

    The execution of the for loop over a list is exactly equivalent to the following while loop.


    loop-list := list;
    loop-index := 1;
    while loop-index <= Length(loop-list) do
    variable := loop-list[loop-index];
    statements
    loop-index := loop-index + 1;
    od;

    with the exception that loop-list and loop-index are different variables for each for loop, i.e., these variables of different for loops do not interfere with each other.

    The list list is very often a range (see Ranges).

  • for variable in [from..to] do statements od;

    corresponds to the more common


    ``for variable from from to to do statements od;'

    in other programming languages.

    gap> s := 0;;
    gap> for i in [1..100] do
    >    s := s + i;
    > od;
    gap> s;
    5050
    

    Note in the following example how the modification of the list in the loop body causes the loop body also to be executed for the new values.

    gap> l := [ 1, 2, 3, 4, 5, 6 ];;
    gap> for i in l do
    >    Print( i, " " );
    >    if i mod 2 = 0 then Add( l, 3 * i / 2 ); fi;
    > od; Print( "\n" );
    1 2 3 4 5 6 3 6 9 9
    gap> l;
    [ 1, 2, 3, 4, 5, 6, 3, 6, 9, 9 ]
    

    Note in the following example that the modification of the variable that holds the list has no influence on the loop.

    gap> l := [ 1, 2, 3, 4, 5, 6 ];;
    gap> for i in l do
    >    Print( i, " " );
    >    l := [];
    > od; Print( "\n" );
    1 2 3 4 5 6
    gap> l;
    [ ]
    

  • for variable in iterator do statements od;

    It is also possible to have a for-loop run over an iterator (see Iterators) . In this case the for loop is equivalent to


    while not IsDoneIterator(iterator) do
    variable := NextIterator(iterator)
    statements
    od;

  • for variable in object do statements od;

    Finally, if an object object which is not a list or an iterator appears in a for-loop, then GAP will attempt to evaluate the function call Iterator(object). If this is successful then the loop is taken to run over the iterator returned.

    gap> g := Group((1,2,3,4,5),(1,2)(3,4)(5,6));       
    Group([ (1,2,3,4,5), (1,2)(3,4)(5,6) ])
    gap> count := 0;; sumord := 0;;
    gap> for x in g do
    > count := count +1; sumord := sumord + Order(x); od; 
    gap> count;
    120
    gap> sumord;
    471
    

    for variable in domain do should thus normally have the same effect as for variable in AsList(domain) do but may use much less storage, as the iterator may be more compact than a list of all the elements.

    See Iterators for details about iterators.

    A for loop may be left prematurely using break, see Break. This combines especially well with a loop over an iterator, as a way of searching through a domain for an element with some useful property.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000