Using the Exit For statement

You can exit from a For/Next loop by using the Exit For statement. The following example searches the Global array names for the name "Fred". If it finds the name, it returns the index of the name in the array. Otherwise it returns -1. For example, if the names array is

Array ("Frank", "Helen", "Fred", "Linda")

Then the formula returns 3.

Global names () As String
'The names array has been initialized and filled
'in other formulas
Dim i
formula = -1
'The UBound function returns the size of its array
'argument
For i = 1 to UBound (names)
   If names (i) = "Fred" Then
      formula = i
      Exit For
   End If
Next i

Do Loops

Another looping mechanism is the Do loop. A Do loop can be used to execute a fixed block of statement an indefinite number of time.

The 4 different types of Do loops
Type of Do Loop Explanation Example

Do While ... Loop

The Do While ... Loop evaluates the condition, and if the condition is true, then it evaluates the statements following the condition.


When it has finished doing this, it evaluates the condition again and if the condition is true, it evaluates the statements again.


It continues repeating this process until the condition is false.

Do While condition
   statements
Loop

Do Until ... Loop

The Do Until ... Loop is similar to the Do While ... Loop except it keeps evaluating the statements until the condition is true rather than while it is true.

Do Until condition
   statements
Loop

Do ... Loop While

The Do ... Loop While evaluates the statements only once.


It then evaluates the condition, and if the condition is true, evaluates the statements again. This process continues until the condition is false.

Do
   statements
Loop While condition

Do ... Loop Until

Similar to Do ... Loop While except that it evaluates the statements until the condition is true.

Do
   statements
Loop Until condition

Note:    The Do loops support an Exit Do statement to immediately jump out of the loop. The Exit Do statement is similar to the Exit For in For/Next loops.



Seagate Software, Inc.
http://www.seagatesoftware.com
Please send comments to:
techpubs@seagatesoftware.com