The Repetition Statement (for...while...do...until/end do)
Repetition Statements within Expressions
| for <name> | | from <expr> | | by <expr> | | to <expr> | | while <expr> |
do <statement sequence> end do;
| for <names> | | in <expr> | | while <expr> |
do <statement sequence> until <expr>;
(Note: Clauses shown between | | above are optional, and can appear in any order, except that the for clause, if used, must appear first.)
The repetition statement provides the ability to execute a statement sequence repeatedly (a loop), either for a counted number of times (using the for...to clauses), while or until a condition is satisfied (using the while or until clause), or for all the elements in a container (using the for...in) clauses. The while or until clauses can be present simultaneously with either of the for clauses.
The optional for name clause specifies the loop's control variable. The name may be any expression for which type(expr, name) is true.
If the for clause is omitted, the loop has an implied control variable. The loop will behave in the same way it would if a control variable had been specified, but the value of the variable is not accessible.
If the from or by clause is omitted, then the default value from 1 or by 1, respectively, is used.
The expr in the while clause is a Boolean expression, which must evaluate to true, false, or FAIL; otherwise, an error occurs. Both false and FAIL can terminate the repetition statement.
The tests to expr and while expr are tested at the beginning of each iteration. Note: In the case of to expr, expr is evaluated only once at the beginning of the loop, and this value is used as the termination test; in the case of while expr, expr is evaluated before every loop iteration.
The expr in the until clause is a Boolean expression, which must evaluate to true, false, or FAIL; otherwise, an error occurs. The repetition statement is terminated when the result is true.
The until expr is tested at the end of each iteration, so in a loop having no other terminating conditions, the body of the loop will be executed at least once.
If an assuming clause is used as part of the until expr, the entire condition, including the assuming clause, must be enclosed in parentheses.
Both the from expr and to expr can be a single-character string, in which case the loop iterates through all the characters in the range as defined by the underlying character representation. The sequence of values that the control variable takes depends on the underlying character set.
In a loop containing for...to clauses and a while clause, the to expr condition is tested first. If the condition is not satisfied, the loop is terminated without testing the while expr condition.
If none of the to expr, while expr, or until expr clauses are present, then the loop is infinite. To exit such a loop use the break statement, a return from a procedure, or the quit statement.
The use of the in expr clause causes the loop variable to take as values the successive operands or entries of the specified expression expr as would be determined using the op command. The exceptions are when expr is an rtable, table, or expression sequence.
For an expression sequence, the loop variable takes as values the operands determined by op([ expr ]).
For a table, the loop variable takes as values the operands determined by map(op, [entries(T)]).
For an rtable, the loop variable may not take the operands in index order.
Note: In the in expr loop, expr is evaluated only once at the beginning of the loop.
A for...in loop can optionally have two loop variables. In this case, during each iteration of the loop, the first variable takes as as value the index of the operand, and the second variable takes on the value of the operand.
The end do that terminates a repetition statement can be shortened to just end, or the ALGOL-like od, if desired.
For more information about loops, including more examples, refer to the Loops section of the Maple Statements chapter of the Maple Programming Guide.
Note about Nested Loops
Statements in a Maple session are recognized in levels, determined by the nesting of conditional or repetition statements and the nesting of procedures. In particular, the top (interactive) level is level 0; statements within conditional and repetition statements are level 1, or level 2 if doubly nested, etc.
The setting of printlevel causes the display of the results of all statements executed up to the level indicated by printlevel. By default, printlevel is initially set to 1. As a result, if nested loops are being used, it may be necessary to set printlevel to a higher value in order to see the results of all the statements in the loops.
If the end do or until expr of a loop entered at the top level is terminated by a colon instead of a semicolon, display of all results (but not explicit output) from within the loop are suppressed, regardless of the setting of printlevel.
When using the in expr clause with a sparse Matrix, Vector or rtable, only the non-zero entries are scanned. Otherwise, regardless of the indexing function, or storage, the entire index-space of the object is scanned. The order of the entries processed is unspecified with these objects.
An alternative method of displaying the result of a statement that is nested within loops is to use the statement in conjunction with the print command.
For more information about displaying information, see printlevel and print.
A repetition statement can be used as an expression or subexpression by enclosing it in parentheses. In the case where such a statement is the only expression within a list, set, or function call, the only expression on the right hand side of an assignment, or the sole argument to a return statement, the parentheses may be omitted.
The value of such an embedded repetition statement is a sequence of the last value computed during each iteration of the repetition.
Storage for the result of repetition expressions is allocated efficiently. In most cases, the amount of storage required can be determined in advance and be pre-allocated. When it cannot be determined in advanced, it is grown in a way that is still O⁡N in the number of iterations, instead of the O⁡N2 cost when appending one element at a time.
Note: Repetition statements embedded within expressions are currently not supported in 2-D input in the Standard interface.
1) Print even numbers from 6 to 10.
for i from 6 by 2 to 10 do print(i) end do;
2) Find the sum of all two-digit odd numbers from 11 to 99.
tot := 0:
for i from 11 by 2 while i < 100 do
tot := tot + i
3) Add together the contents of a list.
for z in [1, x, y, q^2, 3] do
tot := tot + z
4) Multiply the entries of an expression sequence.
tot := 1:
for z in 1, x, y, q^2, 3 do
tot := tot * z
5) Iterate over a range of characters to find the first one that does not occur in a string.
s := "The quick brown fox jumped over the lazy dog.":
for c from "a" to "z" while searchtext(c,s) > 0 do
6) Find the next prime after a given number using the isprime function.
n := 37:
n := n + 1
7) Replace all negative values in a Matrix with their absolute value.
M := LinearAlgebra:-RandomMatrix(3,4):
for ind, val in M do
if val < 0 then
M[ind] := -val
8) Generate a list of all primes less than an N.
N := 30:
[(i := 2), (while (i := nextprime(i)) < N do i end do)];
Note that the examples above don't necessarily illustrate the best way to perform these operations. Often a functional form like seq, map, add, or mul is far more efficient.
Download Help Document
What kind of issue would you like to report? (Optional)