Chapter 7 - Looping in Maple

7.2 Loop Syntax

For Loop

The Maple "for loop" construct specifies how to repeat tasks. The loop specifies that an index is to run from a starting value to an end value by a specific increment. Then it specifies the block of Maple statements which are going to be executed for each value of the loop index. The syntax is:

for index from start to end by increment
do
             block of Maple statements to repeat
end do;

The "block of Maple statements to repeat" specifies the Maple tasks to repeat. "index" is a Maple variable controlling the loop; it can be used in any valid Maple expression within the tasks to repeat. "start", "end", and " increment" specify the "index" range over which the tasks are to be repeated. The key words "do" and "end do" delimit the "block of Maple statements to repeat". The "for loop" construct MUST be written from within a single Maple prompt.

In the above example, the index takes the start value (1); the index is repetitively increased by the specified increment (2) until its value exceeds the end value. For each value of the index, the block of Maple statements (delimited by "do" and "end do") is repeated. The index can be used in any valid Maple expression within the body of the loop (the block of statements to repeat).

lprint & print

The working of a loop often times do not need echoing. For instance, the only thing that really matters for IRS is whether you owe money to them or if they owe money to you; the details of how they arrive at this conclusion are irrelevant (unless there is a dispute, in which case more info is needed). The Maple commands "lprint" and  "print" allow the control of the "printing" from within loops.

 The Maple command "lprint" allows to print messages and content of variables in the output region of the Maple worksheet; it left justifies the displayed information. Printing in this context simply means to display on the screen information that you could otherwise print on a printer; the expression dates back from the early dates of computing when computers could only communicate with their users via printers. Note that expressions must be enclosed within back quotes to print properly.

The "print" commands allows pretty printing, printing with sophisticated fonts, when used instead of the "lprint" command. However, it is a much more powerful command which allows to print procedures, contents of arrays, ...

 

These output commands are useful in dealing with loops since often times the inner workings of loops are not necessary to be echoed, only the final results. The ":" on the "od" statement tells Maple to turn off echoing within the loop. The "lprint" and "print" commands are executed (which means they print) even though the Maple echoing is turn off.

For and While Loops

Another very convenient syntax for the Maple loop is the "while" construct. It basically tells Maple to run a loop compatible with the index range as long as the "while" condition is satisfied. In the example below, the loop is to be run for as long as i<7 (i less than 7). Consequently the loop runs over the same index range as in the example above.

The conditions allowed by Maple are:

  <       smaller than
  <=      smaller than or equal to
  =       equal to
  >=      greater than or equal to
  >       greater than
  <>      not equal to

In addition the "and" and "or" statements can be used to combine conditions. These statements combine conditions according to simple logic rules:

S1 and S2   S1 or S2
S1 S2 result   S1 S2 result
T T T   T T T
T F F   T F T
F T F   F T T
F F F   F F F

Arbitrary ranges for the index can be achieved by carefully choosing the start, end, and increment values. In the following the index is made to run over decreasing values and over negative values.

 


 
Section 7.1 Chapter 7 Section 7.3       TOC

Any questions or suggestions should be directed to
Michel Vallières at vallieres@physics.drexel.edu