VB.NET Loops

For Loop,Do Loop,For Each Loop,Exit and Continue Keywords Within Loops The Exit keyword,The Continue keyword
Share it:
VB.NET Loops
google

Introduction


Loops in VB.NET is a mechanism that lets us repeat a block of code. The number of times the block of code is repeated depends on a condition. There a few different types of loops that suit different situations.


For Loop


The for loop lets you execute a block of code a number of times. This is shown below.


 For i As Integer = 1 To 5 Step 1
            Console.WriteLine("For loop: " & i)
        Next

The for loop starts from i = 1 and terminates when i = 5 (included). The step keywords determine how fast the loop goes up from the lower limit to the upper limit (in our case, 1 and 5 respectively). We specified it to be 1, which means that the loop progresses by 1 each time. We could have omitted this as this is the default behavior. But you could specify step 3, for instance, in which case it i will be 1 (start), 4 (1 + 3), 7 (4 + 3) and so on! This same story is true (but in the opposite direction) when we loop from a higher limit to a lower limit by specifying the step value to be negative.
  Public Sub ForLoopWithMinusStep()
        Dim i As Integer
        For i = 5 To 1 Step -1
            Console.WriteLine("For loop: " & i)
        Next
    End Sub

Do Loop

The do loop can be used in two different ways using two different keywords with it, Do Until and Do While.
The do loop lets you execute a block of code until some condition is reached (Do Until) or while some condition is true (Do While). These variations are implemented using the Until and While keywords, respectively, in the do loop.


 Dim i As Integer = 1 'We start at 1
        Do Until i = 5
            Console.WriteLine("Do until loop: " & i)
            i = i + 1
        Loop

The code below shows the do-while loop. This is exactly like the while loop (that we discussed earlier), but with different syntax.


Dim i As Integer = 1 'We start at 1
        Do While i < 5
            Console.WriteLine("Do while loop: " & i)
            i = i + 1
        Loop

For Each Loop

The for-each loop in VB.NET allows you to loop through a list, array or some other type of collection from start to end. You do not need to check if you have reached the upper limit - it's done automatically. An example of this loop is shown below.


Dim names As New List(Of String)

        names.Add("James")
        names.Add("Joe")
        names.Add("Carl")

        For Each name As String In names
            Console.WriteLine("For each loop: " & name)
        Next

Exit and Continue Keywords Within Loops

The Exit keyword

You can exit from any kind of loop prematurely. You may want to do this because you have found what you were looking for in a list or some other collection, for instance. To exit from a loop, you use the exit keyword followed by the name of the loop as shown below.

exit for (exiting from for loop)
exit while (exiting from while loop)
exit do (exiting from doing until or do-while loops)

As a side note, the exit keyword can also be used to exit from functions (exit function) and/or sub-routines (exit sub). As an example below, the string is not printed when i = 3 because when i = 3 we exit the loop!


For i As Integer = 1 To 5
            If i = 3 Then Exit For
            Console.WriteLine("For loop: " & i)
        Next

The Continue keyword


You can choose to skip the execution of a loop once or multiples times using the continue keyword followed by the name of the loop as shown below.

continue for (continues to the next iteration)
continue while (continues to the next iteration)
continue do (continues to the next iteration in both do until and do-while loops)

As an example below, we do not print the string when i = 3 because when i = 3 we continue to the next iteration i.e. where i = 4.


  For i As Integer = 1 To 5
            If i = 3 Then Continue For
            Console.WriteLine("For loop: " & i)
        Next

Read More

Share it:

VB.NET

VB.NET Loops

Post A Comment:

0 comments: