Controlling program execution MathCad Help

The Programming Palette in Mathcad Professional includes three statements for controlling program execution:

• Use the break statement within the body of the program to halt execution of the program upon the occurrence of a particular condition. Use break also within a for or while loop to stop the loop and move execution to the next statement outside the loop when a particular condition occurs.

• Use the continue statement within a loop to interrupt the current iteration and force program execution to continue with the next iteration of the loop.

• Use the return statement to stop a program and return a particular value from within the program rather than from the last statement evaluated.

The “break” statement

It is often useful to break out of a loop or stop program execution completely upon the occurrence of some condition. For example, there is a possibility of a runaway iteration in the program in Figure 18-6. If every element in v is less than thresh, the condition will never become false and iteration will continue past the end of the vector. This will result in an error message indicating that the index is pointing to a nonexistent array element. To prevent this from occurring, you can use a break statement as shown in Figure 18-7.

The program in Figure 18-7 will return 0 if no elements larger than thresh are found. Otherwise it returns the index and value of the first element exceeding thresh

Example in Figure 18-6 modified to return both the index and

Example in Figure 18-6 modified to return both the index and

To insert the break statement, click on the “break” button in the Programming Palette. Note that to create the program in Figure 18-7, you would click the “break” button first, then click “if’.

Error handling

Errors may occur during program execution that will cause Mathcad to stop calculating the program. For example, because of a particular choice of input, a program may attempt to divide by 0 in some expression and will encounter a singularity error. In these cases Mathcad treats the program as it does other math expressions: it marks the offending expression with an error message and highlights the offending name or operator in a different color, as described in Chapter 7, “Equations and Computation.”

Mathcad Professional gives you two features to improve error handling in programs:

• The on error statement on the Programming Palette allows you to “trap” a numerical error that would otherwise force Mathcad to stop calculating the program.

• The error string function gives you access to Mathcad’s error tip mechanism and lets you customize error messages issued by your program.

“on error” statement

In some cases you may be able to anticipate program inputs that will lead to a numerical error (such as a singularity, an overflow, or a failure to converge) that would force Mathcad to stop calculating the program. As shown in Figure 18-9, the return statement combined with an appropriate boolean test can be used in simple cases to trap values that will lead to program errors. In more complicated cases, especially when your programs rely heavily on Mathcad’s numerical operators or built-in function set, you may not be able to anticipate or enumerate all of the possible numerical errors that can occur in a program. The on error statement is designed as a general-purpose error trap to compute an alternative expression when a numerical error occurs that would otherwise force Mathcad to stop calculating the program.

Custom error messages

Just as Mathcad automatically produces an appropriate “error tip” when you click on an expression that cannot calculate because of an error (see the bottom of Figure 18- 10 for an example), you may want specialized error messages to appear when your programs are used improperly or cannot return answers. Mathcad Professional’s error string function gives you this capability. For example, you can design custom error tips to appear when incorrect arguments are supplied to the program or when particular conditions are encountered in the program.

The error string function, described in the section “String functions” in evaluates to produce an error tip whose text is simply the string expression it takes as an argument. Typically you will use the error string function in the placeholder on the left-hand side of an if or on error programming statement so that the error string function generates an appropriate error tip when a particular condition is encountered.

Figure 18-11 shows how custom error messages can be used even in a small program.

Issuing custom error tips via the error string function.

Issuing custom error tips via the error string function.


Posted on November 24, 2015 in Programming

Share the Story

Back to Top
Share This