Home

Counting and Looping

 

Do While...Loop

Loops are used to repeat an action and they use the Do keyword in combination with other keywords to perform and conditional statement. There are various variations of the Do loops.

The syntax of the Do While loop is:

Do While Condition
    Statement(s)
Loop

The program will first test the Condition. If the Condition is true, the program would execute the Statement or Statements and go back to the Do While statement and test the condition again. This expression will execute the Statement or statements AS LONG AS the Condition is true, as many times as the Condition will be visited and found true. If the Condition is false, the program will skip the Do While statement and not execute any.

Here is an example:

Private Sub cmdCounter_Click()
    Dim Number As Integer
        
    Do While Number < 46
        MsgBox CStr(Number)
        Number = Number + 4
    Loop

    MsgBox "Counting Stopped at " & CStr(Number)
End Sub
 

Do...Loop While

Since the Do While statement tests the Condition first before executing the Statement, sometimes you will want the program to execute the Statement first, then go back and test the Condition. Visual Basic offers a reverse to the syntax, which is:

Do
  Statement(s)
Loop While Condition

In this case, Visual Basic will execute the Statement or Statements first, then it will test the Condition. If the Condition is true, the program will execute the Statement again. The program will continue this examination-execution as long as the Condition is true. The big difference here is that even if the Condition is false, the program will have executed the Condition at least once.

Here is an example:

Private Sub cmdCounter_Click()
    Dim Answer As String

    Do
        Answer = CStr(InputBox("Are we there yet (1=Yes/0=No)?", "Counter", "1"))
    Loop While Answer <> "1"

    MsgBox "Wonderful, we have arrived"
End Sub

Here is an example of running the code:

 

Do Until...Loop

An alternative to the Do While loop is the Do Until loop. Its syntax is:

Do Until Condition
    Statement(s)
Loop

This loop will first examine the Condition, instead of examining whether the Condition is true, it will test whether the Condition is false.

Here is an example:

Private Sub cmdCounter_Click()
    Dim Answer As String

    Do Until (Answer = "1")
        Answer = InputBox("Are we there yet (1=Yes/0=No)?", "Counter", "1")
    Loop

    MsgBox "Wonderful, we have arrived"
End Sub
 

Do...Loop Until

The other side of the Do Until loop would execute the Statement first, then it would examine the Condition. The syntax used is:

Do
    Statement(s)
Loop Until Condition

For...Next

If you don't know how many times a statement needs to be executed, you can use one of the Do loops. But whenever you want to control how many times a statement should be executed, the For...Next loop offers a better alternative. The syntax used is:

For Counter = Start To End
  Statement(s)
Next

Used for counting, the For...Next loop begins counting at the Start point. Then it examines whether the current value (after starting to count) is greater than End; if that's the case, the program exits the loop. It then executes the Statement or Statements. Next, it increments the value of Counter by 1 and examines the condition again. This process goes on until Counter = End.

The syntax above will increment the counting by 1 at the end of each statement. If you want to control how the incrementing processes, you can set your own, using the Step option. Here is the syntax you would use:

For Counter = Start To End Step Increment
  Statement(s)
Next Counter

You can set the incrementing value to your choice. If the value of Increment is positive, the Counter will be added its value. This means that you can give it a negative value, in which case the Counter would be subtracted the set value.

 

Counting and Looping: For...Each

Since the For...Next loop is used to execute a group of statements based on the current result of the loop counting from Start to End, an alternative is to state various steps in the loop and execute a group of statements for each one of the elements in the group. This is mostly used when dealing with a collection of items.

The syntax used is:

For Each Element In Group
    Statement(s)
Next Element

The loop will execute the Statement or Statement(s) for each Element in the Group.

 
 

Previous Copyright © 2005-2016, FunctionX Next