Вы находитесь на странице: 1из 16

.

, -

Mathcad programming

Introduction

A Mathcad program is essentially a multistep function.


In Mathcad, a program is made up of a sequence of statements using special
operators to control the flow of evaluation.
Construct your program, then Mathcad will calculate through the sequence of
statements and return the result of the last step as its answer. It can also return
a value specified by the "return" statement.
Programs can be used to return every data type that you work with in Mathcad:
scalars, arrays, and even text strings.
Mathcad program is created by using its program operators. Mathcad has 10
operators which are accessible from Programming toolbar. You cannot manually
type the programming keywords.

"Add Line"
Operator to initiate a program or to
add a line to a program. When you
click on it, a vertical toolbar and 2
placeholders will be produced. The
Add Line declares a program, it also
declares multiple line expressions
within other program building blocks.

Local assignment operator.


Use the local assignment operator
to assign a value to a variable.
This is similar to the colon equal
sign used in a worksheet;
however, a local assignment
operator is recognized only inside
of the program. All assignments
in a program must be made
using this local assignment
operator.

Conditional operator if
Conditional operator which can be
used whenever you want a
program statement to be executed
only upon the occurrence of some
condition.
In the right placeholder, enter a
Boolean expression.
In the left placeholder, enter a
statement to evaluate if the
Boolean expression is true.
If necessary, use the "Add Line"
operator to insert placeholders for
additional statements.

The otherwise operator


The otherwise operator only
works with the if operator;
when the condition of the if is not
met, Mathcad will execute the
accompanying otherwise
statement. If you use more than
one if statement before an
otherwise statement, the
otherwise statement is executed
only when all previous conditions
are false.
Enter the otherwise statement in
the left placeholder.

For loop
Enter the otherwise statement in the
left placeholder.
Use a for loop when you know exactly
how many times you want the body of
the loop to execute.
Type the iterative variable in the
placeholder on the left, and the range
of values on the right. Place all
calculations to be contained in the
loop immediately under the "for" so
that they are indented.
If necessary, use the the "Add Line"
operator to insert placeholders for
additional statements.

While loop
Use a while loop whenever you want a set of
statements to keep executing while a certain
condition is true. Make sure you have a
statement somewhere that at some point will
make the condition false; otherwise, the loop
will execute indefinitely and you will need to
stop it by pressing [Esc].
In the placeholder to the right of the "while,"
type a Boolean expression.
In the placeholder below the "while," enter the
statement you want to execute repeatedly.
If necessary, use the "Add Line" to insert
placeholders for additional statements.

Return
By default, a program returns the result of
the last executed step. However, you can
return a specific value from anywhere in
the program by using the return operator.
The return statement halts program
execution and returns a specified value.
In the placeholder to the right of the
"return," type whatever you would like to
return.

Example 1 a

Example 1 b

Example 2

Example 3

Example 4

Example 5: for loop

Loop extend shown by indent


Result array built up
Note syntax of for line
Use when you know in advance how
many iterations

Example 6
1+3+5
+ ... + n

Example 7
... + ... +

Вам также может понравиться