Do Loop: Difference between revisions
No edit summary |
|
(No difference)
|
Revision as of 15:30, 26 February 2014
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
Although it is not required to indent the body of a DO LOOP, it is highly recommended to indent it for readability.
Syntax
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
DO LOOP WHILE vs DO WHILE LOOP
There is a major difference in the effect of using DO LOOP WHILE vs DO WHILE LOOP:
DO LOOP WHILE always executes at least once regardless of condition
DO WHILE LOOP may not execute at all if the condition in the WHILE clause is false when the loop attempts to execute for the first time
For example, consider the difference between the following 2 programs:
00010 let a = 1 00020 do 00030 print a ! prints once because while a < 0 is checked after the first loop execution 00040 loop while a < 0 00050 stop
VS
00010 let a = 1 00020 do while a < 0 00030 print a ! doesn't print because while a < 0 is checked before the first loop execution 00040 loop 00050 stop
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.
Common Errors
Not providing in the body of a DO LOOP an action that eventually causes the condition in the WHILE clause to become false or the condition in the UNTIL clause to become true normally results in a logic error called an infinite loop, in which the loop never terminates.
The following example will execute forever:
00010 let a = 100 00020 do 00030 print a 00040 loop while a > 0 00050 ! the program never gets here, because the value of a is 100, which is ALWAYS greater than 0 00060 stop
The program above appears to be stuck in RUN mode, until the user forcefully terminates the program using CTRL+A and the STOP command (or another command which stops program execution)
Below is what a corrected version of the above program might look like:
00010 let a = 100 00020 do 00030 print a 00040 let a -= 1 00050 loop while a > 0 00060 ! the program gets here after 100 executions of the while loop, 00070 ! after which a is no longer greater than 0 00080 stop
Loop Performance
In loops, avoid calculations for which the result never changes—such calculations should typically be placed before the loop.