INTRODUCTION TO MAPLE
Teresa Dern Henson, Lecturer
Department of Mathematics
Naval Postgraduate School
September, 1995


SECTION 4
WRITING DO-LOOPS IN MAPLE

GOALS:

In this section we will learn how to write a do-loop.

DO-LOOPS

do-loops. The Repetition ( for / while / do) Statement allows you to execute a statement repeatedly either for some finite number of times (for) or until some condition has been satisfied (while). The structure for a Repetition Statement which iterates a finite number of times is

where k is the statement variable (the index), a is the initial value of k, h is the amount by which k is to be incremented at each iteration, and b is an upper bound for k. The increment h may be non-integer. The do-loop terminates when k > b; after the do-loop is executed k will be assigned the value that it had when the do-loop terminated. The word for signals the start of the loop while the word od signals the end of the input. To execute a do-loop all lines must be entered sequentially starting with the first. No output will be computed or displayed until the od line is entered.

The statement to be executed must be a valid Maple expression; the statement line should end in a semicolon. Several statements may be executed within one do-loop.

If the do-loop ends with a colon, then the output will be computed but not displayed. You may include a print statement within the do-loop if you wish to display some part of the output but not all of it.

EXAMPLE:  Construct a do-loop which will evaluate the function f(x) = x2 for x = 1, x =3/2, x = 2, x = 5/2, ..., x = 5.
Solution: Assign the function f. The initial value for x is a = 1. The upper bound for x is b = 5. The increment is h = 1/2.

After executing these lines enter The statement variable k is now assigned the value 11/2 > 5 = b. If you wish to reiterate or use k in some other context, you must first "unassign" k by entering EXAMPLE:  Unassign f before trying this example Suppose in the last example we wanted to get output in the form  where k is an x value. To do this we must "fool" Maple. By assigning the function expression x2 to y, no functional meaning is attached to the expression f(k) within the do-loop.

DON'T FORGET TO UNASSIGN k after executing the do-loop.

EXAMPLE:  Unassign y before trying this example.  As an alternative to using functional notation, you can use subscripted variables. To create a subscripted variable yk in Maple use a variable name of the form y[k]. This approach has the advantage of allowing you to assign the output from the do-loop to a subscripted variable.

 In this example the right-hand side of each output is assigned to the left-hand side. Thus the Maple input will compute 2y3/2 + 1 using the output y3/2 from the do-loop.
 
EXAMPLE:  If you define the function using function mapping notation, then function evaluation notation may be used within the do-loop instead of the subs function.  DON'T FORGET TO UNASSIGN k after executing the do-loop.

 TO TABLE OF CONTENTS