VB.NET tutorial: Loops-For...Next loop


VB.NET Loops

Loops execute a block of code repeatedly while a condition is true. There are five types of loops in VB.NET--For Next, For Each, While, Do While, and Do Until loops.

For Next loop

The For Next loop executes the statements from an initial value to the uper limit value. You can use the Step keyword to specify the updating step of the loop.

For initial value To end value Step value
            statements
Next
 
Example:
 
Module Module1

 
    Sub Main()

 
        Dim i As Integer
        'print numbers from 1 to 10 on the screen
        For i = 1 To 10 Step 1
            Console.WriteLine(i)
        Next
        Console.ReadLine()

 
    End Sub
 
End Module

For Each loop

For Each loop is used to loop through a collection of items (e.g. array). Its general form is shown below.


For each variable in collection
            statements
Next
 
Example:
Module Module1

 
    Sub Main()
        'create and initialize the array
        Dim arr() As Integer
        arr=New Integer(2){}
        arr(0) = 1
        arr(1) = 2
        arr(2) = 3
        'loop through the array
        For Each item In arr
            Console.WriteLine(item)
        Next
        Console.ReadLine()
    End Sub
 
End Module

 

While loop

The while loop continuously executes statements as the condition is true. You must provide end point for the loop, otherwise the statements will executed infinitely.

While condition
   statements

End while

 


Example:
Module Module1
 
    Sub Main()
 
        Dim i As Integer
        i = 1
        'print numbers from 1 to 5 on the screen
        While i <= 10
            Console.WriteLine(i)
            i = i + 1
            If i > 5 Then
                Exit While 'exit loop when i is greater than 5
            End If
        End While
        Console.ReadLine()
    End Sub
 
 
End Module
 

Note: Exit statement is used to exit a loop

 

Do While loop

Syntax 1:

Do while condition

            statements

loop

 

Example:

Module Module1

 

    Sub Main()

        Dim i As Integer

 

       

        Do While i <= 10

            Console.WriteLine(i)

            i += 1

        Console.ReadLine()

 

    End Sub

 

End Module

 

Syntax 2:

Do

            statements

loop while condition

 

Example:

 

Module Module1

 

    Sub Main()

        Dim i As Integer

        i=0 

        Do

            Console.WriteLine(i)

            i += 1

        Loop While i <= 10

        Console.ReadLine()

 

    End Sub

 

End Module

 

Do Until loop


Syntax 1:
Do until condition
            statements
loop
 
Example:
 
Module Module1
 
    Sub Main()
        Dim i As Integer
        i = 1
        'print numbers from 1 to 10 on the screen
        Do Until i > 10
            Console.WriteLine(i)
            i = i + 1
        Loop
        Console.ReadLine()
 
    End Sub
 
Syntax 2:
Do
            statements
loop until condition
 
Example:
Module Module1
 
    Sub Main()
        Dim i As Integer
        i = 1
        'print numbers from 1 to 10 on the screen
        Do
            Console.WriteLine(i)
            i = i + 1
        Loop Until i > 10
        Console.ReadLine()
 
    End Sub
 
End Module
 
Note: Both Syntax1 of do while and do until loops won't execute any thing if the condition is not true. In contrast, their Syntax2 will execute the block of code one time if the condition is not true.




Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.