Do...Loop: Difference between revisions
Created page with "'''Statement''' <pre> DO [WHILE | UNTIL ''condition''] [''statements''] [EXIT DO] [''statements''] LOOP or DO [''statements''] [EXIT DO] [''statements'']..." |
No edit summary |
||
Line 1: | Line 1: | ||
== Statement == | |||
<pre> | <pre> | ||
Do [While | Until ''condition''] | |||
[''statements''] | [''statements''] | ||
[EXIT | [EXIT Do] | ||
[''statements''] | [''statements''] | ||
Loop | |||
or | or | ||
Do | |||
[''statements''] | [''statements''] | ||
[EXIT | [EXIT Do] | ||
[''statements''] | [''statements''] | ||
Loop [While | Until ''condition''] | |||
</pre> | </pre> | ||
== Description == | |||
Do...Loop repeats a block of statements While a given condition is TRUE, or UNTIL a given condition becomes TRUE. The required component, ''condition'', is any expression that can be evaluated as TRUE or FALSE. The optional component, ''statements'', are the statements executed during the body of the loop. Any number of optional EXIT Do statements can be used to exit a loop before it is finished. Do...Loop statements can be nested, and any EXIT DO statements in a nested loop transfer execution to one level above the loop where the EXIT Do occurs. | |||
Placing the | Placing the While/Until clause directly after Do causes ''condition'' to be evaluated before the first loop is executed, if ''condition'' is FALSE statements are never executed; placing the While/Until clause after LOOP causes the body of the loop to be executed before ''condition'' is evaluated, statements will always be executed, at least once. | ||
== Example == | |||
<pre> | <pre> | ||
REM | REM Do...Loop Example | ||
' | 'Do...Loop repeats a block of statements | ||
Dim Counter | |||
Counter = 1 | Counter = 1 | ||
'Loop that prints 1 to 5 | 'Loop that prints 1 to 5 | ||
Do While Counter < 6 | |||
PRINT " | PRINT "Do While...Loop:", Counter | ||
Counter = Counter + 1 | Counter = Counter + 1 | ||
Loop | |||
PRINT | PRINT | ||
'Infinite loop that uses EXIT DO toterminate | 'Infinite loop that uses EXIT DO toterminate | ||
Counter = 1000 | Counter = 1000 | ||
Do | |||
PRINT "Infinite Loop:", Counter | PRINT "Infinite Loop:", Counter | ||
IF Counter >= 1000000 THEN | IF Counter >= 1000000 THEN | ||
EXIT DO | EXIT DO | ||
Counter = Counter * 10 | Counter = Counter * 10 | ||
Loop | |||
</pre> | </pre> | ||
== Output == | |||
<pre> | <pre> | ||
Do While...Loop: 1 | |||
Do While...Loop: 2 | |||
Do While...Loop: 3 | |||
Do While...Loop: 4 | |||
Do While...Loop: 5 | |||
Infinite Loop: 1000 | Infinite Loop: 1000 | ||
Infinite Loop: 10000 | Infinite Loop: 10000 | ||
Line 68: | Line 68: | ||
</pre> | </pre> | ||
== Related Items == | |||
[[exit|EXIT]], [[for each...next|FOR EACH...NEXT]], [[for...next|FOR...NEXT]], [[while...wend|WHILE...WEND]] | [[exit|EXIT]], [[for each...next|FOR EACH...NEXT]], [[for...next|FOR...NEXT]], [[while...wend|WHILE...WEND]] | ||
[[Category:Language Reference]] |
Revision as of 20:58, 8 August 2012
Statement
Do [While | Until ''condition''] [''statements''] [EXIT Do] [''statements''] Loop or Do [''statements''] [EXIT Do] [''statements''] Loop [While | Until ''condition'']
Description
Do...Loop repeats a block of statements While a given condition is TRUE, or UNTIL a given condition becomes TRUE. The required component, condition, is any expression that can be evaluated as TRUE or FALSE. The optional component, statements, are the statements executed during the body of the loop. Any number of optional EXIT Do statements can be used to exit a loop before it is finished. Do...Loop statements can be nested, and any EXIT DO statements in a nested loop transfer execution to one level above the loop where the EXIT Do occurs.
Placing the While/Until clause directly after Do causes condition to be evaluated before the first loop is executed, if condition is FALSE statements are never executed; placing the While/Until clause after LOOP causes the body of the loop to be executed before condition is evaluated, statements will always be executed, at least once.
Example
REM Do...Loop Example 'Do...Loop repeats a block of statements Dim Counter Counter = 1 'Loop that prints 1 to 5 Do While Counter < 6 PRINT "Do While...Loop:", Counter Counter = Counter + 1 Loop PRINT 'Infinite loop that uses EXIT DO toterminate Counter = 1000 Do PRINT "Infinite Loop:", Counter IF Counter >= 1000000 THEN EXIT DO Counter = Counter * 10 Loop
Output
Do While...Loop: 1 Do While...Loop: 2 Do While...Loop: 3 Do While...Loop: 4 Do While...Loop: 5 Infinite Loop: 1000 Infinite Loop: 10000 Infinite Loop: 100000 Infinite Loop: 1000000