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 A | Clears sum register |
0003 | `# OF ENTRIES?` | Creates message |
0016 | BRK | Stops for input |
0017 | STO B | Stores # of entries |
0019 | STO C | Stores loop count |
0021 | LBL AA | Labels segment |
0024 | `ENTER NUMBER` | Creates message |
0036 | BRK | Stops for input |
0037 | ST+ A | Sums entry |
0039 | DSZ C | Does count=0? |
0041 | GTL AA | No-repeat loop |
0044 | RCL B ST/ A | Yes-calculate average |
0048 | RCL A | Recalls average |
0050 | HLT | Stops program |
Running the Example
Run the program and calculate the average of 121 and 9.Procedure | Press | Display |
---|---|---|
Run the program | [ RUN ] { PGM } | |
Enter the count | 2 | |
Continue | { GO } | |
Enter first number | 121 | |
Continue | { GO } | |
Enter second number | 9 | |
Continue, display average | { GO } |
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 |
---|---|---|
0000 | LBL AA | Begins loop |
0003 | `ENTER VALUE` | Creates message |
0014 | BRK | Waits for entry |
0015 | STO A | Stores for comparison |
0017 | 0 IF= A | Does entry = 0? |
0020 | HLT | Yes-terminate loop |
0021 | RCL A y^x 4 = | No-calculate power |
0026 | PAU PAU | Pause for 2 seconds |
0028 | GTL AA | Repeats loop |
Running the Example
Enter the program and run it.Procedure | Press | Display |
---|---|---|
Start the program | [ RUN ] { PGM } | |
Enter a value | 2.3 { GO } | |
Enter a value | 1.89 { GO } | |
Enter terminating value | 0 { GO } |
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 A | Stores the loop counter |
0003 | LBL AA | Begins loop |
0006 | `ENTER VALUE` | Creates message |
0017 | BRK | Waits for entry |
0018 | STO B | Stores for comparison |
0020 | 0 IF= B | Does entry = 0? |
0023 | HLT | Yes-terminate loop |
0024 | RCL B y^x 4 = | No-calculate power |
0029 | PAU PAU | Pause for 2 seconds |
0031 | DSZ A | Does count = 0? |
0033 | GTL AA | No-repeate loop |
0036 | HLT | Yes-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