In general, Mathcad evaluates each statement in your program from the top down. There may be times, however, when you want Mathcad to evaluate a statement only when a particular condition is met. You can do this by including an if statement in your program. Note that the if statement in a Mathcad program is not the same as the if function, which you use elsewhere in your worksheet. The if function is described in For example, suppose you want to define a function that forms a semicircle around the origin but is otherwise constant. To do this:
• Type the left side of the function definition, followed by a “: =”. Make sure the placeholder is selected.
• Click the “Add Line” button on the Programming Palette. Alternatively, press]. You’ll see a vertical bar with two placeholders. These placeholders will hold the statements making up your program.
• In the top placeholder, click the “if’ button on the Programming Palette. Alternatively, press} .
• In the remaining placeholder, type the value you want the program to return if the condition in the first statement is not met
Figure 18-3 shows a plot of this function. Note that since this function only has two branches, it’s not hard to define it using the if function as shown in Figure 18-3. However, as the number of branches exceeds two, the if function rapidly becomes unwieldy. An example is shown in Figure 18-4.
One of the greatest strengths of programmability is the ability to execute a sequence of statements over and over again in a loop. Mathcad provides two such loops. The choice of which loop to use depends on how you plan to tell the loop to stop executing
• If you know exactly how many times a loop is to execute, you can use a for loop.
• If you want the loop to stop upon the occurrence of a condition, but you don’t know when that condition will occur, use a while loop.
A for loop is a loop that terminates after a predetermined number of iterations. Iteration is controlled by an iteration variable defined at the top of the loop. While in most cases you want a for loop to calculate for the complete number of iterations, you may also interrupt the loop on the occurrence of a particular condition. In such cases stop execution within the body of the loop using one of the methods described in “Controlling program execution” on page 407.
To create a for loop
• Click the button labeled “for” on the Programming Palette. Do not type the word “for”
•In the placeholder to the left of the “E ,” type the name of the iteration variable.
The upper half of Figure 18-5 shows this for loop being used to add a sequence of integers. The undefined variable in Figure 18-5 shows that the definition of an iteration variable is local to the program. It has no effect anywhere outside the program.
The lower half shows an example in which the iteration variable is defined not in terms of a range but in terms of the elements of a vector. Although the expression to the right of the “E” is usually a range, it can also be a vector, a list of scalars, ranges and vectors separated by commas.
A while loop is driven by the truth of some condition. Because of this, you don’t need to know in advance how many times the loop will execute. It is important, however, to have a statement somewhere, either within the loop or elsewhere in the program, that
eventually makes the condition false. Otherwise, the loop will execute indefinitely. If you do find yourself in an endless loop or want to interrupt the loop on the occurrence of a particular condition, you can stop execution within the body of the loop using one of the methods described in “Controlling program execution” on page 407
To create a while loop
• Click the button labeled “while” on the Programming Palette
• In the remaining placeholder, type the expression you want evaluated repeatedly. If necessary, add placeholders by clicking the
“Add Line” button on the Programming Palette.
Figure 18-6 shows a larger program incorporating the above loop. Upon encountering a whiIe loop, Mathcad checks the condition. If the condition is true, Mathcad executes the body of the loop and checks the condition again. If the condition is false, Mathcad exits the loop.