Share via


Loops with Visual Basic.Net

What the heck is a loop?

A loop is a repetitive procedure that is designed to repeat a given set of instructions.

Loops are absolutely essential for getting work done. You know how computer software saves you so much time, by doing the tedious repetition of tasks? Our good hero the loop is the one to blame.

Other terms associated with loops: Looping, Iterating, Iteration, Enumerating, Enumeration, Cycling

What kind of loops can you make in Visual Basic?

GOTO

The most primitive loop (GOTO) in the basic programming language involves the use of 2 components:

  1. A numeric label for the parser
  2. A GOTO command

Example of a simple GOTO Loop

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
1:  Dim Counter As  Integer = 0
2:  Counter = Counter + 1
3:  'You can put
4:  'code in-between
5:  If Counter < 100 Then GoTo 2
6:  MsgBox("We have reached "  & Counter & ", so the loop is complete!")
 End Sub
End  Class

Never use this, there are better ways!

Do-Loop

The good ol' Do-Loop Loop, this loop has several different ways it can be used:

Do-Loop-Until

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Count As  Integer = 0
 Do
 'You can put
 'Code in here
 Count = Count + 1
 Loop  Until Count = 100
 MsgBox("We have reached " & Count & ", so the loop is complete!")
 End  Sub
End Class

Do-Loop-While

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Count As  Integer = 0
 Do  While Count < 100
 'You can put
 'Code in here
 Count = Count + 1
 Loop
 MsgBox("We have reached " & Count & ", so the loop is complete!")
 End  Sub
End Class

Do-Loop-Exit Do

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Count As  Integer = 0
 Do
 'You can put
 'Code in here
 Count = Count + 1
 If  Count = 100 Then  Exit Do
 Loop
 MsgBox("We have reached " & Count & ", so the loop is complete!")
 End Sub
End  Class

There's yet another kind of loop! For-Next

For - Next

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Result As  Integer
 For  I As  Integer = 0 To 100
 'You can put
 'Code in here
 Result = I
 Next
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End  Sub
End Class

For-Next-Step

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Result As  Integer
 For  I As  Integer = 500 To 100 Step  -2
 'You can put
 'Code in here
 Result = I
 Next
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End  Sub
End Class

For-Next-Exit For

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Result As  Integer
 For  I As  Integer = 0 To 500000
 'You can put
 'Code in here
 Result = I
 If  I = 100 Then  Exit For
 Next
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End Sub
End  Class

For-Next-Continue For

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Result As  Integer
 For  I As  Integer = 0 To 200
 Select Case I Mod 2
 Case  1
 Result = Result + 1
 Continue For
 End Select
 'Only arrive here when I is an odd number
 Next
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End Sub
End  Class

For-Each-Next

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Array() As  Integer = {25, 25, 25, 25}
 Dim  Result As  Integer
 For Each Number As Integer  In Array
 Result = Result + Number
 Next
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End Sub
End  Class

While

While-End While

 

Example

Option Strict  On
Public Class Form1
 Private  Sub Button1_Click(ByVal sender As  System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
 Dim Result As  Integer
 While  Result < 100
 'You can put
 'Code in here
 Result = Result + 1
 End  While
 MsgBox("We have reached " & Result & ", so the loop is complete!")
 End  Sub
End Class

Last, but not least, I want to add a very interesting method of looping called recursion.

I first really noticed it with Euclid's algorithm for calculating the greatest common divisor of 2 numbers. So here it is, Euclid's Algorithm utilizing recursion. This function will call itself recursively until the remainder is 0, effectively creating a loop without using any of the conventional methods listed above!

Example

Function EuclidGCD(Number1 As  Integer, Number2 As Integer)
 Dim Remainder As  Integer = Number1 Mod Number2
 If  Remainder = 0 Then  Return Number2
 Return EuclidGCD(Number2, Remainder)
End  Function

Summary

There are many many uses of looping in Visual basic, and programming in general would not be complete without the ability to iterate.

Credits

Please view my other Technet Wiki Articles