Do Loop
DO [WHILE|UNTIL condition1] [loop body] [exit do] LOOP [WHILE|UNTIL condition2]
A DO LOOP allows you to specify that an application should repeat a statement or set of statements while some condition1 remains true and/or until another condition2 becomes true.
In the above syntax, condition1, condition2, the loop body, and the exit do statement are optional, but the use of at least one of them is encouraged for clarity.
The loop body may consist of zero or more statements. However, the use of at least one statement in the loop body is encouraged for clarity.
Eventually, the condition(s) should become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes.
The DO LOOP stops execution in 3 cases:
- The condition following the WHILE keyword becomes FALSE
- The condition following the UNTIL keyword becomes TRUE
- An EXIT DO statement is encountered
Examples
For example, the loop below will print 3, then double the 3 to 6 and print it, then double the 6 to 12 and print it, and so on until the variable becomes greater than 100. At this point the repetition stops.
00010 let a=3 00020 do until a > 100 00030 print a 00040 let a = 2 * a 00050 loop
You can reach an identical result by using WHILE and reversing the condition from a > 100 to a <= 100:
00010 let a=3 00020 do while a <= 100 00030 print a 00040 let a = 2 * a 00050 loop
You may want to combine the use of both WHILE and UNTIL in the same DO LOOP:
00010 do while fkey <> 99 ! loop will stop if user presses ESC 00020 let kstat$(1) 00030 print "FKEY is now "&str$(FKEY) 00040 loop until fkey = 12 ! loop will stop if user presses F12
Results identical to those from the above example may be obtained from the following DO LOOP:
00010 do while fkey <> 99 and fkey <> 12 ! loop will stop if user presses ESC or F12 00020 let kstat$(1) 00030 print "FKEY is now "&str$(FKEY) 00040 loop
EXIT DO
00010 do 00020 print 'This loop will stop when you press F1' 00030 let kstat$(1) 00040 if FKEY = 1 then EXIT DO 00050 loop
As you can see, the combination of WHILE, UNTIL, and EXIT DO allows for various combinations of DO LOOP conditions.