Looping statements

The most general from of loop in Maple is the following:

[> for var from first to last by increment while some-while-condition do
    # Loop body
end do:
[> for var from first to last by increment while some-while-condition do
    # Loop body
until some-ending-condition;

You can also have a loop that executes once for each entry in a list of set:

[> for var in list-or-set while some-while-condition do
    # Loop body
end do:
[> for var in list-or-set while some-while-condition do
    # Loop body
until some-ending-condition;

The simplest loop is an infinite loop, which generally is not really useful, but you can break out of a loop by executing a break; command.

[> do
    # Start of loop body
    if some-end-condition then
        break;
    end;
    # End of loop body
end do:

If you simply want a loop to execute N times where N is an integer greater than or equal to $1$, you can do the following:

[> to N do
    # Loop body
end do:

There is an implied loop variable that is initially assigned 1 and it is incremented by one at the end of each execution of the loop body.

If you want to have a loop variable that is assigned the count, you can use the following, where with the first iteration of the loop, k is assigned the value $1$, and at the end of the loop body, the value of k is incremented:

[> for k to N do
    # Loop body
end do:

# The value of 'k' will be 'N + 1' at this point

Now, an interesting little detail: you can change the value of k inside the loop if you want so for example:

[> for k to N do
    if some-condition-met then
        # Reset 'k' to 1 (or whatever value you want
        k := 1;
    end if;

    # Loop body
end do:

# The value of 'k' will be 'N + 1' at this point

You can also change the initial value, and the step size or increment size:

[> for k from 3 to 20 by 4 do
    # Loop body
end do:

In this example, k will take on the values $3$, $7$, $11$, $15$, $19$, and that's it, for the next number, $23 > 20$.

You can also have a simple while loop:

[> while some-while-condition do
    # Loop body
end do:

or a loop-until statement (a negated do-while loop in C++):

[> do
    # Loop body
until some-ending-condition;

A for-in loop does not suffer from the same issues with the incremental version: if you want to manipulate the loop variable, but always have it reset to the next value, you can use:

[> for k in [seq( 1..20 )] do
    # Loop body
end do;

You can now manipulate the loop variable, but this loop will execute exactly twenty times, and the loop variable will progressively be assigned the integers $1$ through $20$ through all the iterations.

One question you may have is why are there so many variations on these looping statements? After all, could you not author any of these loops using, for example:

[> k := 3;
while k <= 30 do
    # Loop body
    k += 2;
end do:

You must remember, however, that Maple is interpreted, and therefore using the variation of built-in for loops will be significantly more efficient.