MATHCAD'S PROGRAMMING LANGUAGE
The for Loop
______________________________________
Introduction
In this section we look at the for loop construct in Mathcad
programming. A loop is a means of performing a sequence of similar
operations repeatedly over some range of values.
Looping outside of programs can be accomplished with a range
variable. When a Mathcad document is treated as a program, the
entire worksheet can be iterated by external scripting of
variables.
Range variables can be used in many ways, such as filling an array,
i 0 20
Xi sin( i )
accumulating values contained in a specific array,
sum_of_cubes0 0
sum_of_cubes0 i sum_of_cubes0 i Xi
3
sum_of_cubes ( 0.769 )
or performing more complicated iterations over large ranges,
A0 1 B0 0.5
j 1 1000
A j 1
Aj j Bj 1
2
j
Bj
exp mod Aj 1 10 Bj 1
Range variables can also be used in a programmatic sense with the
until function, demonstrated earlier.
These computations may be part of a sequence of steps that lend
themselves to a program definition. As we mentioned in the section
on assignments, defining range variables is not allowed within a
program. The construct there is the for loop.
The for Loop Operator
Clicking on the for button in the Programming toolbar creates an
operator that looks like
for
Note: As with the if operator we emphasize that you cannot type
this operator. You must use the button.
Before we give the details, here are some simple uses of the for loop
in a program.
This function creates an array specifying N equal subintervals of the
interval [a, b]
ba
Partition ( a b N) Δ
N
for i 0 N
Pi a i Δ
P
0
0
0.2 1
0.4
Partition ( 0 1 5) Partition ( 0 4 4) 2
0.6 3
0.8
4
1
The next function adds the reciprocals of the nonzero elements
in a vector:
rsum ( V) sum 0
for x V
sum sum 0 if x = 0
1
otherwise
x
sum
1
0
1
rsum 2 1.833 rsum 1 1
3 0
1
So, there you see for in action. Here are the details.
The first placeholder in the for operator,
iteration variable
|
for
is reserved for the name of a variable that we'll refer to as the
iteration variable:
for i for k
for x
The second placeholder contains a set of values that the iteration
variable will take on over the course of the for loop.
range, list of values, vector
|
for
This set of values can be specified with a range
for i 0 10
The range may be increasing, decreasing, have a specified increment,
or use the default increment (decrement) of 1 (-1).
Alternatively, the set of values may be specified simply as a list of
numbers separated by commas.
for k 17 3 5 9
Lastly, the set of iteration variable values may be the elements of a vector
1.2
V
9.0
2.3
8.1
for x V
Or some combination of the above may be used, separated by commas.
The final placeholder contains an expression or set of expressions,
that is, a subprogram that you wish executed for each value of the
loop variable in the specified range.
for
|
set of expressions
These will usually be local assignment statements but do not have to be.
Here are some examples of completed for loops.
for i 0 10
ti i
for k 17 3 5 9
sum sum k
prod prod k
for x V
sign sign 1 if x 0
sign sign 1 if x 0
sign sign otherwise
Execution of a for loop then performs the indicated computations in
the set of expressions for each value of the loop variable in the order
specified. However, this execution is only of interest (or of use, for
that matter) within a program.
0
0 0
1 1
2 1.414
3 1.732
for i 0 10 4 2
5 2.236
ti i 6 2.449
7 2.646
8 2.828
9 3
10 3.162
You shouldn't need to use the for loop outside of a program, mainly
because the assignments inside a for loop must be local. Range
variables in conjunction with worksheet-level vector/matrix
assignments should do what is required at the worksheet level. Note
that the t assigned above is local only, and not available in the
worksheet.
t
Examples of programs using the for loops above are
r for i 0 10 r0 0 r5 2.236
ti i
r1 1
t
Q sum 0
prod 1 24
Q
for k 17 3 5 9 3
2.295 10
sum sum k
prod prod k
sum
prod
p sign 0
for x V
sign sign 1 if x 0
sign sign 1 if x 0
sign sign otherwise
p2
In the case of a range specified with a vector, the order of values the
iteration variable takes on is from the first element to the last in sequence.
The symbol
is usually read as "element of" or simply "in" and for loops such as
for i 0 10 for x 1 1.2 3
might be read as "for i in the set 0 to 10 do ...." and "for x from 1 to 3
with steps of 0.2 do the following..." respectively.
Note: The three methods described that define the range of iteration may
be combined into a single list. Just separate the pieces with commas.
For example, the for loop in
q 0 221.9
2.3
16
for w 1.5 5 8 1 ( 10 12 20)
2
0.1
10
q q w
q
performs the exact same sequence of iterations as
q 0 221.9
2
for w 2.3 1.5 0.1 5 8 1 16 10 10 12 14 16 18 20
q q w
q
or
W 2.3 1.5 0.1 5 8 1 16 10
2
10 12 14 16 18 20 T
q 0 221.9
for w W
q q w
q
Note in specifying the for values
2.3
16
1.5 5 8 1 2 ( 10 12 20)
0.1 10
we enclosed the range piece
10 12 20
in parentheses. Using parentheses is a practice we highly recommend.
The comma that appears in a range definition means something
different from the comma used to separate values in our iteration value
list. These different meanings can lead to confusion.
For example at a glance, the list
1 4 6 10
could be a list of the values 1 and 4 followed by the range 6 10
meaning a total set of values:
1 4 6 7 8 9 10
or the list could mean the single value 1 followed by the range
4 6 10 meaning the set:
1 4 6 8 10
Parentheses remove any ambiguity.
1 4 ( 6 10) 1 ( 4 6 10)
Examples Using for Loops
As you might guess, for loops are especially useful when working
with arrays.
Our first function takes a function of two variables f as an argument
and an integer N and returns an (N+1) x (N+1) grid of values of f
over the square
0x10y1
fgrid ( f N) for i 0 N
for j 0 N
Si j f
i j
N N
S
F ( x y) sin ( x) cos ( y)
1 0.98 0.921 0.825 0.697 0.54
1.199 1.179 1.12 1.024 0.895 0.739
1.389 1.369 1.31 1.215 1.086 0.93
fgrid ( F 5)
1.565 1.545 1.486 1.39 1.261 1.105
1.717 1.697 1.638 1.543 1.414 1.258
1.841 1.822 1.763 1.667 1.538 1.382
Note in this program a for loop lives inside another for loop. There is
nothing wrong with that. For the first value of the outer iteration variable
i 0
the innermost for loop is executed in its entirety, that is
for j 0 N
S0 j f
0 j
N N
then the next value of the outer iteration variable is used,
i 1
for j 0 N
S1 j f
1 j
N N
and so on. Note that this construct means the array S is filled up a row at a
time.
Our next example locates all occurrences of a number in a vector returning
a vector of indices or the number -1 if no occurrences are found.
locate ( x V) count 0
for i 0 last ( V)
if x = Vi
Icount i
count count 1
I if count 0
1 otherwise
0.3
9 0.4
0
2
locate 1 1 locate 0.3 0.3 2
0 0.3 3
6
1
Our next example computes the Cartesian product of two vectors.
Given two vectors of any sizes, the Cartesian product is the set of all
points (vectors of two elements) that can be formed by taking an
element from the first vector and an element from the second.
CartProd ( X Y) ind 0
for x X
for y Y
x
Pind
y
ind ind 1
P
0.1
3
C CartProd 0.5
1 0.7
T 3 3 3 1 1 1
C
0.1 0.5 0.7 0.1 0.5 0.7
The two previous examples illustrate how easy it is to define functions
that operate on vectors and arrays of unknown size by using for loops.
The following example demonstrates that you can iterate over a
vector of matrices. The function Prod takes a vector of arrays V and
returns the array that is the product of all the arrays in V.
Prod ( V) P identity rows V0
for A V
P PA
P
1 4 9
2 1
2
0.1 1.8
V0 6 8 5 V1 2.3 3.8
0 0.1 5 5.1 4.4
1 3 3
2.3
V2 V3 ( 1 3 10 4 )
1.7
12.04 36.12 120.4 48.16
34.33 102.99 343.3 137.32
V0 V1 V2 V3 95.51 286.53 955.1 382.04
20.075 60.225 200.75 80.3
51.29 153.87 512.9 205.16
12.04 36.12 120.4 48.16
34.33 102.99 343.3 137.32
Prod ( V) 95.51 286.53 955.1 382.04
20.075 60.225 200.75 80.3
51.29 153.87 512.9 205.16
More examples of for loops appear in the section Examples of
Programming in Mathcad.