VB.NET Loops

There are three main types of Loops in VB.NET, the while loops, for loops and do while loop. There is also a variant of the for loop, called the For Each loop. Click on the links to check individual articles on each loop, which deal with each loop in more detail.

StatementDescription
whileRepeats a block a code while a given condition holds true.
do whileExecutes a block of code once, and will keep repeating as long as a given given condition holds true.
for Repeats a block of code for a given number of times. Can also be used to iterate through collection types variables, like Lists and Arrays.

VB.NET Loop control statements:

Loop control statement help us manipulate Java loops during their execution.

NameDescription
breakWhen a loop encounters this statement during its execution, it will immediately stop and “break”, moving to the next statement after the loop.
continueThe loop will stop it’s current iteration, and move on to the next iteration.
GoToShifts the program flow to a specified statement. Using this isn’t recommended however as it can disrupt the program flow considerably.

While Loops Example

While the condition x < 5 holds true, the loop will continue running. Once the condition x < 5 becomes false, the loop terminates.

Module Module1
    Sub Main()
        Dim x As Integer
        x = 0

        While (x < 5)
            Console.WriteLine(x)
            x = x + 1
        End While
        Console.Read()

    End Sub
End Module

Do While Loops Example

It is a variation of the While loop. Unlike the while loop, it will first execute the statements in the code block, and then proceed to check whether the given condition is true or false. If it was true, it will proceed to repeat itself.

Module Module1
    Sub Main()
        Dim x As Integer
        Dim sum As Integer = 0

        Do
            x = Console.ReadLine()
            sum = sum + x
        Loop Until (x = -1)
        Console.WriteLine(sum)
        Console.Read()

    End Sub
End Module
8
8
9
5
3
5
-1
37

For Loops Example

The loop repeats 10 times, from 1 to 10, printing out each value with each iteration.

Module Module1
    Sub Main()
        For x = 1 To 10
            Console.WriteLine(x)
        Next
        Console.Read()


    End Sub
End Module
1
2
3
4
5
6
7
8
9
10

For Each loops Example

This for loop iterates over the Array x, printing out each value individually. The number of times the loop repeats is the number of values in it.

Module Module1
    Sub Main()
        Dim x As Array
        Dim element As Integer

        x = {3, 4, 5, 7}
        For Each element In x
            Console.WriteLine(element)
        Next
        Console.Read()

    End Sub
End Module
3
4
5
7

Nested Loops

A Nested loop is a loop within another loop. Using Nested loops in your code can prove very useful and help you iterate through sequences in new ways. The most common use of Nested loops comes in iterating through other Nested or multi-dimensional items like 2-D arrays.

What’s happening here is that there is a Array, which further contains two Arrays within it. This is what we call a two dimensional Array. If we iterate through it normally, we’ll just get 2 Arrays printed out, which you can even see in the example output. Iterating through the Array with two for loops gives us each element separately.

Module Module1
    Sub Main()
        Dim a As Array
        a = {{5, 3}, {3, 5}, {4, 1}, {3, 3}}

        For x = 0 To 3
            For y = 0 To 1
                Console.WriteLine(a(x, y))
            Next
        Next
        Console.Read()

    End Sub
End Module

Keep an eye on the sequence of the numbers printed to screen.

5
3
3
5
4
1
3
3

This marks the end of the VB.NET Loops section. Any suggestion or contributions are more than welcome. Any relevant questions can be asked in the comment section below.