DO...LOOP


Control flow statement for looping

Syntax:
Do [ { Until | While } condition ]
[ statement block ]
Loop
or
Do
[ statement block ]
Loop [ { Until | While } condition ]

Description:
The Do statement executes the statements in the following statement block until/while the condition, if any, evaluates to true.

If Until is used, the Do statement stops repetition of the statement block when condition evaluates to true. The While keyword has opposite effect, stopping the loop if condition evaluates to false. If both condition and either Until or While are omitted, the Do statement loops indefinitely.

If an Exit Do statement is encountered inside the statement block, the loop is terminated, and execution resumes immediately following the enclosing Loop statement. If a Continue Do statement is encountered, the rest of the statement block is skipped and execution resumes at the Do statement.

In the first syntax, the condition is checked when the Do statement is first encountered, and if the condition is met, the statement block will be skipped. In the second syntax, condition is initially checked after the statement block is executed. This means that the statement block is always guaranteed to execute at least once.

condition may be any valid expression that evaluates to False (zero) or True (non-zero).

Examples:
In this example, a Do loop is used to count the total number of odd numbers from 1 to 10. It will repeat until its n > 10 condition is met:
Dim As Integer n = 1                            '' number to check
Dim As Integer total_odd = 0                    '' running total of odd numbers
Do Until( n > 10 )
  If( ( n Mod 2 ) > 0 ) Then total_odd += 1    '' add to total if n is odd (has remainder from division by 2)
  n += 1
Loop
Print "total odd numbers: " ; total_odd         '' prints '5'

End 0

Here, an infinite DO loop is used to count the total number of evens. We place the conditional check inside the loop via an If...Then statement, which exits the loop if and when n > 10 becomes true:
   Dim As Integer n = 1                            '' number to check
   Dim As Integer total_even = 0                   '' running total of even numbers
   Do
      If( n > 10 ) Then Exit Do                    '' exit if we've scanned our 10 numbers
   
      If( ( n Mod 2 ) = 0 ) Then total_even += 1   '' add to total if n is even (no remainder from division by 2)
      n += 1
   Loop
   Print "total even numbers: " ; total_even       '' prints '5'

   End 0


Dialect Differences:
Differences from QB:
See also:
Back to Control Flow
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode