How to: Run Several Statements Repeatedly

You can use loop structures to run a block of statements repeatedly. The loop can run an indeterminate number of times, depending on the Boolean value of a condition, or for a set number of times controlled by a special variable.

Looping an Indeterminate Number of Times

To run a group of statements as long as a condition is True

  • Use the While...End While Statement (Visual Basic) to specify the condition that controls repetition of the loop. The following example repeats the statement block as long as number is greater than 6.

    Sub checkWhile()
        Dim counter As Integer = 0
        Dim number As Integer = 10
        While number > 6
            number -= 1
            counter += 1
        End While
        MsgBox("The loop ran " & counter & " times.")
    End Sub
    

    The While statement always checks the condition before it begins the loop. If number had been initialized to 6 instead of 10, the statements inside the loop would never run.

To run a group of statements while a condition remains True

  • Use the Do...Loop Statement (Visual Basic) and specify the testing condition at either the beginning or the end of the loop. The placement of the While (Visual Basic) keyword determines where the condition is tested. The following example illustrates this.

    Sub checkWhileFirst()
        Dim counter As Integer = 0
        Dim number As Integer = 10
        Do While number > 6
            number -= 1
            counter += 1
        Loop
        MsgBox("The loop ran " & counter & " times.")
    End Sub
    Sub checkWhileLast()
        Dim counter As Integer = 0
        Dim number As Integer = 5
        Do
            number -= 1
            counter += 1
        Loop While number > 6
        MsgBox("The loop ran " & counter & " times.")
    End Sub
    

    In the preceding example, the first Do loop runs four times and the second Do loop runs one time.

To run a group of statements until a condition becomes True

  • Use the Do...Loop construction with the Until keyword instead of While. As with While, the placement of the keyword determines where the condition is tested. The following example illustrates this.

    Sub checkUntilFirst()
        Dim counter As Integer = 0
        Dim number As Integer = 20
        Do Until number = 15
            number -= 1
            counter += 1
        Loop
        MsgBox("The loop ran " & counter & " times.")
    End Sub
    Sub checkUntilLast()
        Dim counter As Integer = 0
        Dim number As Integer = 20
        Do
            number -= 1
            counter += 1
        Loop Until number = 15
        MsgBox("The loop ran " & counter & " times.")
    End Sub
    

    In the preceding example, each Do loop runs five times.

Looping a Set Number of Times

While and Do loops work well when you do not know in advance how many times you need to run the statements in a loop. However, when you expect to run the loop a specific number of times, the For...Next Statement (Visual Basic) is a better choice. Unlike a While or Do loop, a For...Next loop uses a control variable that increases or decreases in value during each repetition of the loop.

To run a group of statements a set number of times

  1. Determine the starting and ending values of the control variable, and use the For statement to specify them.

    For i As Integer = 1 To 10
    

    If the control variable is not declared outside the loop, you can use the As clause to declare it as part of the For statement.

  2. Use the Step keyword to indicate the amount the control variable should increase for each iteration. It increases by 1 unless you specify otherwise. Use a negative value to cause the control variable to decrease.

    For i As Integer = 10 To 1 Step -1
    
  3. Complete the For...Next construction with a Next statement following the last statement to be repeated. You can specify the control variable in the Next statement.

    Function addBackward(ByVal highest As Integer) As Integer
        Dim total As Integer = 0
        For i As Integer = highest To 1 Step -1
            total += i
        Next i
        Return total
    End Function
    

    The preceding example returns the sum of all the whole numbers from 1 through the value passed to the parameter highest.

See Also

Tasks

How to: Transfer Control Out of a Control Structure

How to: Run Several Statements for Each Element in a Collection or Array

How to: Improve the Performance of a Loop

How to: Skip to the Next Iteration of a Loop

Concepts

Decision Structures

Loop Structures

Other Control Structures

Nested Control Structures

Reference

For Each...Next Statement (Visual Basic)

Other Resources

Control Flow in Visual Basic