Visual Basic Loops

A loop statement allows for the execution of a statement or group of statements multiple times. There are three types of loop statement in Visual Basic.

  • ‘For’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
  • ‘While’ loop – Repeats a statement or group of statements while an expression evaluates to True. There is a condition to get into the loop, so if the condition evaluates to True at the beginning then the statements inside the loop will never execute.
  • ‘Do’ loop – Allows for a condition to be tested either at the beginning or end of the loop structure. It is also possible to repeat the loop while the condition is True or until the condition becomes True.

As with ‘If’ statements, it is possible to nest one or more of any of these loops inside another.

‘For’ Loop

The ‘For’ loop is useful when the statements within the loop are required to run a specific number of times and this is known in advance. It takes the following form.

For counter = start To end
    ' Statement(s) to execute.
Next

The ‘counter’ is like a variable, except that it doesn’t need to be defined first and can have any name. The ‘start’ and ‘end’ values refer to the initial value of the ‘counter’ and the point at which the loop stops, so, for example, ‘start’ and ‘end’ values of one and ten would mean that the statements within the loop would be executed ten times.

The following example displays the value of the ‘counter’ in the console. As the ‘start’ and ‘end’ values for the ‘counter’ are one and ten, the numbers one through to ten will be displayed on separate lines in the console.

For i = 1 To 10
    Console.WriteLine(i)
Next

The ‘Step’ keyword can also be included in a ‘For’ loop. This has the effect of increasing the ‘counter’ by the specified value, instead of incrementing by one each time through the loop, as in the above example.

For i = 1 To 10 Step 2
    Console.WriteLine(i)
Next

Here, only the numbers one, three, five, seven and nine are displayed on separate lines in the console because the counter increments by two each time.

The ‘Continue For’ and ‘Exit For’ statements can be used to exit the current iteration and exit the ‘For’ loop completely.

For i = 1 To 10

    If i = 4 Then
        Continue For
    ElseIf i = 9 Then
        Exit For
    End If

    Console.WriteLine(i)

Next

Here the ‘Continue For’ statement exits the fourth iteration of the loop before the counter is output to the console. On the ninth iteration through the loop the ‘Exit For’ statement is encountered, causing the program to exit the ‘For’ loop completely before the counter is output to the console, with the tenth iteration of the loop not occurring at all. The output in the console will be as follows.

1
2
3
5
6
7
8

‘While’ Loop

As mentioned above, the ‘While’ loop has a condition, which must evaluate to true before the statements within it can be executed.

While condition  
    ' Statement(s) to execute. 
End While 

In the below example, a variable is declared and initialised to zero, then the ‘While’ loop checks to see that the variable is less than or equal to ten, before outputting it to the screen and incrementing the variable by one.

Dim i As Integer = 1
While i <= 10
    Console.WriteLine(i)
    i += 1
End While

The output will be the same as with the first ‘For’ loop example, with the numbers one through to ten being displayed in the console on separate lines. Note that if ‘i’ had been initialised to eleven or more then nothing would be output to the console because the statements within the loop would never execute.

Similar to the ‘For’ loop, there are ‘Continue While’ and ‘Exit While’ statements to either exit the current iteration of the loop and continue with the next, or exit the loop completely.

Dim i As Integer = 0
While i <= 10

    i += 1

    If i = 4 Then
        Continue While
    ElseIf i = 9 Then
        Exit While
    End If

    Console.WriteLine(i)

End While

As with the last ‘For’ loop example, the numbers one through to eight will be displayed in the console, except the number four because the ‘Continue While’ statement exits the fourth iteration through the loop before the number is output. The ‘Exit While’ statement exits the ‘While’ loop completely on the ninth iteration through the loop before the number is output to the console, which also stops the tenth iteration from happening at all.

‘Do’ Loop

A ‘Do’ loop comes in a number of different forms. It can either be used to execute a statement or group of statements whilst a condition is True, or until a condition is True. It is also possible to have a condition to get in to the loop, or at the end.

This first example executes the statements within the loop while the variable ‘i’ is less than or equal to ten. Here, ‘i’ is initialised to one, then within the loop its value is output to the console before being incremented by one. The result of this is that the numbers one to ten are displayed on separate lines in the console. Note that if the variable ‘i’ is initialised to eleven or more then the statements within the loop would never be executed.

Dim i As Integer = 1
Do While i <= 10
    Console.WriteLine(i)
    i += 1
Loop

The difference between the below example and the one above is that the program executes until the condition becomes True. The output is exactly the same. Again, if the variable ‘i’ is initialised to eleven or more then the statements within the loop will never get executed.

Dim i As Integer = 1
Do Until i > 10
    Console.WriteLine(i)
    i += 1
Loop

With the last two examples below, the condition is at the end of the loop, which means that regardless of what the variable ‘i’ is initialised to, the statements within the loop will always execute at least once, because the condition doesn’t get tested for the first time until the end of the first iteration.

Here the statements within the loop execute while the condition is True, in this case, while the variable ‘i’ is less than or equal to ten. The output in the console is exactly the same as in the above two examples. Note that if ‘i’ is initialised to eleven or more, then one value would be output to the console because the condition isn’t tested until the end of the first iteration.

Dim i As Integer = 1
Do
    Console.WriteLine(i)
    i += 1
Loop While i <= 10

The final example, which produces the same results as the other three, executes the statements within the loop until the condition is True, in this case, until ‘i’ is greater than ten. As with the above example, if ‘i’ were initialised to eleven or more, then one value would be displayed in the console before the condition is checked.

Dim i As Integer = 1
Do
    Console.WriteLine(i)
    i += 1
Loop Until i > 10

As with the ‘For’ and ‘While’ loops, there are similar ‘Continue’ and ‘Exit’ statements, in this case, ‘Continue Do’ and ‘Exit Do’, that work with the condition at the beginning or the end and with ‘While’ or ‘Until’.

Here the variable ‘i’ is initialised to zero, but it does get incremented by one at the start of the loop. When the value of ‘i’ gets to four, the ‘Continue Do’ statement is encountered, meaning that the remaining statements in the loop for this iteration are not executed, therefore the value ‘four’ is not output to the console. When ‘i’ reaches nine, the ‘Exit Do’ statement gets executed, terminating the ‘Do’ loop, therefore only the values one through to eight, except four, are displayed in the console.

Dim i As Integer = 0
Do
    i += 1

    If i = 4 Then
        Continue Do
    ElseIf i = 9 Then
        Exit Do
    End If

    Console.WriteLine(i)

Loop Until i > 10