 do - Maple Help

The Repetition Statement (for...while...do...until/end do) Calling Sequence | for  | | from  | | by  | | to  | | while  | do  end do; OR | for  | | in  | | while  | do  end do; OR | for  | | from  | | by  | | to  | | while  | do  until ; OR | for  | | in  | | while  | do  until ; (Note: Clauses shown between | | above are optional, and can appear in any order, except that the for clause, if used, must appear first.) Description

 • 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 successive entries or operands of the specified expression, expr, corresponding to expr[i] or op(i,expr), depending on whether the expression is indexable. The expression is only evaluated once, before the first iteration of the loop.
 – If expr is an expression sequence, it is treated as if it were a list. That is, for x in a,b,c is equivalent to for x in [a,b,c].
 – When expr is a table, the order in which the entries are processed depends on the underlying storage order, which may not correspond to any natural order (such as numeric or alphabetic order).
 – When expr is an rtable (Array, Vector, or Matrix), the entries are processed in underlying storage order. For dense rectangular tables, this will be row-major (when order=C_order) or column-major (order=Fortran_order) order. For other forms of rtables, the order is unspecified.
 – For string expressions, the loop iterates over the individual characters of the string.
 – For expressions having neither entries nor operands, expr is treated as a 1-element list containing only that expression.
 • A for...in loop can optionally have two loop variables. In this case, during each iteration of the loop, the first variable takes as value the index of the entry, or operand number of the operand. The second variable takes on the corresponding value.
 If the second variable is the name _ (a single underscore), this indicates that the value is not needed, and Maple may choose to not look it up, and not assign a value to _.
 • 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.
 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. Repetition Statements within Expressions

 • 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 $\mathrm{O}\left(N\right)$ in the number of iterations, instead of the $\mathrm{O}\left({N}^{2}\right)$ 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. Examples

1) Print even numbers from $6$ to $10$.

 > for i from 6 by 2 to 10 do  print(i)  end do;
 ${6}$
 ${8}$
 ${10}$ (1)

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 end do: tot;
 ${2475}$ (2)

3) Add together the contents of a list.

 > tot := 0:
 > for z in [1, x, y, q^2, 3] do  tot := tot + z end do: tot;
 ${{q}}^{{2}}{+}{x}{+}{y}{+}{4}$ (3)

4) Multiply the entries of an expression sequence.

 > tot := 1:
 > for z in 1, x, y, q^2, 3 do   tot := tot * z end do: tot;
 ${3}{}{x}{}{y}{}{{q}}^{{2}}$ (4)

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 end do; c;
 ${"s"}$ (5)

6) Find the next prime after a given number using the isprime function.

 > n := 37:
 > do   n := n + 1 until isprime(n): n;
 ${41}$ (6)

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   end if end do;
 > M;
 $\left[\begin{array}{cccc}{32}& {27}& {99}& {92}\\ {74}& {8}& {29}& {31}\\ {4}& {69}& {44}& {67}\end{array}\right]$ (7)

8) Generate a list of all primes less than an N.

 > N := 30:
 > [(i := 2), (while (i := nextprime(i)) < N do i end do)];
 $\left[{2}{,}{3}{,}{5}{,}{7}{,}{11}{,}{13}{,}{17}{,}{19}{,}{23}{,}{29}\right]$ (8)

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.