Using a Test to Exit a Loop


You can use a test instruction within a loop as a means of terminating the loop. You can design a loop so that it terminates after a specified number of repetitions, when a specified condition exists, or when either of those two events occurs.


Exiting on a Specified Count

A DSZ or INV DSZ test is commonly used to repeat a loop a specified number of times.

Example

Write a program that calculates the average of numbers that you enter. Design the program so that you can specify how many numbers you want to average.

PC =

Program Mnemonics

Comments

0000 0 STO AClears sum register
0003 `# OF ENTRIES?`Creates message
0016 BRKStops for input
0017 STO BStores # of entries
0019 STO CStores loop count
0021LBL AALabels segment
0024 `ENTER NUMBER`Creates message
0036 BRKStops for input
0037 ST+ ASums entry
0039 DSZ CDoes count=0?
0041 GTL AANo-repeat loop
0044 RCL B ST/ AYes-calculate average
0048 RCL ARecalls average
0050 HLTStops program


Running the Example

Run the program and calculate the average of 121 and 9.

Procedure

Press

Display

Run the program[ RUN ]
{ PGM }
D_TKLgdn
Enter the count2D_6YkwKb
Continue{ GO }D_xGrgPV
Enter first number121D_Nl8mmw
Continue{ GO }D_xGrgPV
Enter second number9D_Dv9AfP
Continue, display average{ GO }D_CyDLP7



Exiting on a Specified Condition

In many instances, you want to exit a loop only if a specified condition exists. For example, you may want to repeat a loop until a variable in the program is equal to zero.

Example

The following program raises values you enter to the fourth power. The program repeats the loop indefinitely until you enter a value of 0.

PC =

Program Mnemonics

Comments

0000LBL AABegins loop
0003 `ENTER VALUE`Creates message
0014 BRKWaits for entry
0015 STO AStores for comparison
0017 0 IF= ADoes entry = 0?
0020 HLTYes-terminate loop
0021 RCL A y^x 4 =No-calculate power
0026 PAU PAUPause for 2 seconds
0028 GTL AARepeats loop



Running the Example

Enter the program and run it.

Procedure

Press

Display

Start the program[ RUN ]
{ PGM }
D_foZPNg
Enter a value2.3 { GO }D_TJ7GAp
Enter a value1.89 { GO }D_1ciZsj
Enter terminating value0 { GO }D_4i4c7p



Exiting on Count or Condition

You may want to design a loop that terminates when either a specified count is reached, or some other condition occurs. Such a loop must contain more than one test.

The following program calculates the fourth power of numbers you enter. The program combines the methods used in the two previous examples for exiting from a loop.
  • It uses a loop counter to limit the number of entries to a maximum of five.
  • It tests for an entered value of 0. This lets you terminate the loop before you have entered all five values.

PC =

Program Mnemonics

Comments

0000 5 STO AStores the loop counter
0003LBL AABegins loop
0006 `ENTER VALUE`Creates message
0017 BRKWaits for entry
0018 STO BStores for comparison
0020 0 IF= BDoes entry = 0?
0023 HLTYes-terminate loop
0024 RCL B y^x 4 =No-calculate power
0029 PAU PAUPause for 2 seconds
0031 DSZ ADoes count = 0?
0033 GTL AANo-repeate loop
0036 HLTYes-terminate loop


Running the Example

Run the program and enter five numbers. The program terminates after it calculates the fourth power of the fifth number.

Run the program a second time and enter 0 as one of the numbers. The program terminates when you enter the 0.

Back