Programming Tutorial > Task 3–2: Writing While Loops
  
Task 3–2: Writing While Loops
Writing a Basic While Function
Write a function sigma that sums all the numbers from 0 to n.
1. Define a function sigma that accepts a variable n and create a new program.
Click to copy this expression
2. To add an empty while loop, on the Math tab, in the Operators and Symbols group, click Programming, and then click while.
Click to copy this expression
3. Specify that the while loop should continue as long as n>0 and should decrement n by 1 inside the loop.
Click to copy this expression
* 
Unlike for loops, you must increment or decrement the while iterator.
4. To add the current iterator value to the sum, type the following line as shown below, right after the while statement.
Click to copy this expression
5. Return the value of sum.
Click to copy this expression
* 
Update the iterator last; otherwise, you miss the first iteration addition.
6. Calculate the value of sigma for 5.
Click to copy this expression
As expected, the program is equivalent to the following sum:
Click to copy this expression
Adding Continue Statements
Add continue statements to keep the loop running but to skip a specific iteration.
Write a function that sums all the numbers from 0 to n, except for numbers that can be divided by 17.
1. Copy the above function and rename it to sigma_not17.
Click to copy this expression
2. Inside the while loop, add a new line under the while statement.
Click to copy this expression
3. Add an if statement and type the expression below.
Click to copy this expression
4. To avoid endless looping, decrement n by 1.
5. To add a continue statement, on the Math tab, in the Operators and Symbols group, click Programming, and then click continue.
Click to copy this expression
6. Calculate the value of sigma_not17 for 16 and 17.
Click to copy this expression
Click to copy this expression
Adding Break Statements
Write a program that sums all numbers and exits the loop when the counter is greater than 20.
1. Define a variable sum and create a new program.
Click to copy this expression
2. To add an empty while loop, on the Math tab, in the Operators and Symbols group, click Programming, and then click while.
Click to copy this expression
3. Specify that the while loop should run forever.
Click to copy this expression
* 
A while loop runs as long as the value of the expression inside the parenthesis is not 0.
4. Initialize sum and i.
Click to copy this expression
5. Add the value of the iterator i to a variable sum and increment i by 1 inside the loop.
Click to copy this expression
6. Return the value of sum.
Click to copy this expression
* 
Currently this loop is infinite.
7. To break the loop, type if i > 20 and add the break statement. To add a break statement, on the Math tab, in the Operators and Symbols group, click Programming, and then click break.
Click to copy this expression
8. Calculate sum.
Click to copy this expression
9. To break the loop and exit the program, select the break statement and on the Math tab, in the Operators and Symbols group, click Programming, and then click return, to modify it to a return statement. Type sum in the placeholder as shown below.
Click to copy this expression
* 
Use return to immediately exit a program.
Practice
Before you move to the next task, write a function fact(n) that implements a factorial function using a while loop. Define the loop to run as long as n is greater than 1. Inside the loop, multiple n by a variable product (saving the factorial result) and decrement n by 1.
* 
In PTC Mathcad, program variables are set to 0 by default. In the beginning of the program, you must assign 1 to product. Otherwise, the program will yield 0 for all arguments.
Proceed to Task 3–3