Loop Structures

Loop structures allow you to execute one or more lines of code repetitively. The loop structures that Visual Basic supports include:

Do...Loop

You can use a Do loop to execute a block of statements an indefinite number of times. There are several variations of the Do...Loop statement, but each evaluates a numeric condition to determine whether to continue execution. As with If...Then, the condition must be a value or expression that evaluates to False (zero) or to True (nonzero).

In the following Do...Loop, the statements execute as long as the condition is True:

Do While condition
statements
Loop

When Visual Basic executes this Do loop, it first tests condition. If condition is False (zero), it skips past all the statements. If it’s True (nonzero), Visual Basic executes the statements and then goes back to the Do While statement and tests the condition again.

Consequently, the loop can execute any number of times, as long as condition is nonzero or True. The statements never execute if condition is initially False. For example, this procedure counts the occurrences of a target string within another string by looping as long as the target string is found:

Function CountStrings (longstring, target)

Dim position, count

position = 1

Do While InStr(position, longstring, target)

position = InStr(position, longstring, target) + 1

count = count + 1

Loop

CountStrings = count

End Function

Another variation of the Do...Loop statement executes the statements first and then tests condition after each execution. This variation guarantees at least one execution of statements:

Do
statements


Loop While condition

Two other variations are analogous to the previous two, except that they loop as long as condition is False rather than True.

Ø Loop zero or more times

Do Until condition
statements
Loop

Ø Loop at least once

Do
statements
Loop Until condition

It should be noted that the Do Until condition is equivalent to Do While Not condition.

For...Next

Do loops work well when you don’t know how many times you need to execute the statements in the loop. When you know you must execute the statements a specific number of times, however, your code is more efficient if you use a For loop. Unlike a Do loop, a For loop uses a counter variable that increases or decreases in value during each repetition of the loop. The syntax is:

For counter = start To end [Step increment]
statements
Next [counter]

The arguments counter, start, end, and increment are all numeric.

It is important to note that the increment argument can be either positive or negative. If increment is positive, start must be less than or equal to end or the statements in the loop will not execute. If increment is negative, start must be greater than or equal to end for the body of the loop to execute. If Step isn’t set, then increment defaults to 1.

In executing the For loop, Visual Basic:

Ø Sets counter equal to start.

Ø Tests to see if counter is greater than end. If so, Visual Basic exits the loop. (If increment is negative, Visual Basic tests to see if counter is less than end.)

Ø Executes the statements.

Ø Increments counter by 1 — or by increment if it’s specified.

Ø Repeats steps 2 through 4.

This code prints the names of all the available Screen fonts:

Private Sub Form_Click ()

Dim i

For i = 0 To Screen.FontCount - 1

Print Screen.Fonts(i)

Next

End Sub


Each...Next

A For Each...Next loop is similar to a For...Next loop, but it repeats a group of statements for each element in a collection of objects or in an array, instead of repeating the statements a specified number of times. This is especially helpful if you don’t know how many elements are in a collection.

Here is the syntax for For Each...Next:

For Each element In group
statements
Next element

For example, the following Sub procedure opens BIBLIO.MDB and adds the name of each table to a list box.

Dim objDb As Database

Set objDb = OpenDatabase("c:\vb\biblio.mdb", True, False)

For Each TableDef In objDb.TableDefs()

List1.AddItem TableDef.Name

Next TableDef

You must keep the following restrictions in mind when using For Each...Next:

Ø For collections, element can only be a Variant variable, a generic Object variable, or an object listed in the Object Browser.

Ø For arrays, element can only be a Variant variable.

Ø You cannot use For Each...Next with an array of user-defined types because a Variant cannot contain a user-defined type.

While...Wend Statement


Executes a series of statements as long as a given condition is True.

The While...Wend statement syntax has these parts:

Part

Description

condition

Numeric or string expression that evaluates to True or False. If condition is Null, condition is treated as False.

statements

One or more statements executed while condition is True.

if condition is True, all statements in statements are executed until the Wend statement is encountered. Control then returns to the While statement and condition is again checked. If condition is still True, the process is repeated. If it is not True, execution resumes with the statement following the Wend statement.

While...Wend loops can be nested to any level. Each Wend matches the most recent While.


Tip The Do...Loop statement provides a more structured and flexible way to perform looping.

The following example illustrates use of the While...Wend statement:

Dim Counter

Counter = 0

While Counter < style="">

Counter = Counter + 1

Alert Counter

Wend


No comments:

Post a Comment