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

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Flow Control : Iteration or Looping : do

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

 

Calling Sequence

Description

Examples

Calling Sequence

| for <name> | | from <expr> | | by <expr> | | to <expr> | | while <expr> |

do <statement sequence> end do;

OR

| for <name> | | in <expr> | | while <expr> |

do <statement sequence> 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.

• 

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 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.

Examples

1) Print even numbers from 6 to 10.

forifrom6by2to10doprintiend do

6

8

10

(1)

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

tot0&colon;

forifrom11by2whilei<100dotottot&plus;iend do&colon;tot

2475

(2)

3) Add together the contents of a list.

tot0&colon;

forzin1&comma;x&comma;y&comma;q2&comma;3dotottot&plus;zend do&colon;tot

q2&plus;x&plus;y&plus;4

(3)

4) Multiply the entries of an expression sequence.

tot1&colon;

forzin1&comma;x&comma;y&comma;q2&comma;3dotottotzend do&colon;tot

3xyq2

(4)

5) Iterate over a range of characters to find the first one that does not occur in a string.

sThe quick brown fox jumped over the lazy dog.&colon;

forcfromatozwhile0<searchtextc&comma;sdoend do&semi;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.

See Also

add

boolean

break

map

mul

next

op

print

printlevel

quit

return

seq

statement

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam