Do Loop: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
DO [WHILE '' | DO [WHILE|UNTIL ''condition1''] | ||
[loop body] | [loop body] | ||
LOOP [WHILE|UNTIL '' | LOOP [WHILE|UNTIL ''condition2''] | ||
A '''DO LOOP''' allows you to specify that an application should repeat an | A '''DO LOOP''' allows you to specify that an application should repeat an statement or set of statements while some '''condition''' remains true and/or until another '''condition''' is true. | ||
Eventually, the condition '''should''' become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes. | In the above syntax, both '''condition1''' and '''condition2''' are optional, but the use of at least one of them is encouraged for clarity. | ||
The '''loop body''' consists of zero or more statements. | |||
Eventually, the condition(s) '''should''' become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes. | |||
'''Note''' that if your DO LOOP '''condition''' is never false, your DO LOOP may repeat infinitely. In this case, the program stays ''stuck'' in RUN mode. | '''Note''' that if your DO LOOP '''condition''' is never false, your DO LOOP may repeat infinitely. In this case, the program stays ''stuck'' in RUN mode. | ||
====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 in order to separate two unrelated conditions |
Revision as of 15:29, 9 February 2012
DO [WHILE|UNTIL condition1] [loop body] LOOP [WHILE|UNTIL condition2]
A DO LOOP allows you to specify that an application should repeat an statement or set of statements while some condition remains true and/or until another condition is true.
In the above syntax, both condition1 and condition2 are optional, but the use of at least one of them is encouraged for clarity.
The loop body consists of zero or more statements.
Eventually, the condition(s) should become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes.
Note that if your DO LOOP condition is never false, your DO LOOP may repeat infinitely. In this case, the program stays stuck in RUN mode.
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 in order to separate two unrelated conditions