omron — Looping Structures

Looping Structures 46 47 x = 1 48 DO 49 PROMPT "Enter a character: ", $ans 50 TYPE $ans 51 x = x + 1 52 UNTIL (x > 15) OR ($ans == "#") 53 . In this code, either x reaching 15 or # being entered at the PROMPT instruction terminates the loop. As long as the operator enters enough characters, the loop terminates. WHILE...DO WHILE...DO is a looping structure similar to DO...UNTIL except the Boolean expression is evaluated at the beginning of the loop instead of at the end. This means that if the condition indicated by the expression is true when the WHILE...DO instruction is encountered, the code within the loop will be executed.

WHILE...DO loops are susceptible to infinite looping just as DO...UNTIL loops are. The expression controlling the loop must eventually evaluate to true for the loop to terminate. The form of the WHILE...DO looping structure is: WHILE expression DO code block END expression is any well-formed Boolean expression as described at the beginning of this section. The following code shows a WHILE...DO loop being used to validate input. Since the Boolean expression is tested before the loop is executed, the code within the loop will be executed only when the operator inputs an unacceptable value at step 23. 20 .

21 ; Loop until operator inputs value in the range 32-64 22 23 PROMPT "Enter a number in the range 32 to 64.", ans 24 WHILE (ans 32) OR (ans > 64) DO 25 PROMPT "Number must be in the range 32-64.", ans 26 END 27 . In the above code, an operator could enter a nonnumeric value, in which case the program execution would stop. A more robust strategy would be to use a string variable in the PROMPT instruction and then use the $DECODE and VAL functions to evaluate the input. In the following code, if digital signal 1033 is on when step 69 is reached, the loop does not execute, and the program continues at step 73.

If digital signal 1033 is off, the loop executes continually until the signal comes on. 65 . 66 ; Create a busy loop waiting for signal eV+Language User's Guide, v2.x, 18318-000 Rev A Page 74