Mathcad - Inside Mathcad - Programming - The For Loop PDF
Mathcad - Inside Mathcad - Programming - The For Loop PDF
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.
i 0 20
Xi sin( i )
accumulating values contained in a specific array,
sum_of_cubes0 0
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.
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.
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.
for i 0 10
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
for
|
set of expressions
These will usually be local assignment statements but do not have to be.
for i 0 10
ti i
for x V
sign sign 1 if x 0
sign sign 1 if x 0
sign sign otherwise
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
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
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.
0x10y1
Si j f
i j
N N
S
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.
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.
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
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