The Repetition Statement (for...while...do) - Maple Help

The Repetition Statement (for...while...do)

 Calling Sequence | for  | | from  | | by  | | to  | | while  | do  end do; OR | for  | | in  | | while  | do  end do; (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, either for a counted number of times (using the for...to clauses) or until a condition is satisfied (using the while clause). Both forms of clauses can be present simultaneously.
 • 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.
 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 neither the to expr nor the while expr clause is 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 index 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 index variable takes as values the operands determined by op([ expr ]).
 – For a table, the index variable takes as values the operands determined by map(op, [entries(T)]).
 – For an rtable, the index 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.
 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 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.

Examples

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

 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{from}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}6\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{by}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}2\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}10\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{print}\left(i\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}$
 ${6}$
 ${8}$
 ${10}$ (1)

2) Find the sum of all two-digit odd numbers from 11 to 99.

 > $\mathrm{tot}:=0:$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{from}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}11\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{by}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}2\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{while}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}i<100\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{tot}:=\mathrm{tot}+i\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$$\mathrm{tot}$
 ${2475}$ (2)

3) Add together the contents of a list.

 > $\mathrm{tot}:=0:$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}z\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[1,x,y,{q}^{2},3\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{tot}:=\mathrm{tot}+z\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$$\mathrm{tot}$
 ${{q}}^{{2}}{+}{x}{+}{y}{+}{4}$ (3)

4) Multiply the entries of an expression sequence.

 > $\mathrm{tot}:=1:$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}z\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}1,x,y,{q}^{2},3\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{tot}:=\mathrm{tot}z\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do}:$$\mathrm{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.":$
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}c\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{from}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}"a"\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}"z"\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{while}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}0<\mathrm{searchtext}\left(c,s\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathbf{end do};$$c$
 ${"s"}$ (5)

Note that the examples above don't necessarily illustrate the best way to perform these operations. Often a function like seq, map, add, or mul is far more efficient.