For Next Loop in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes how to use the For Next loop in Visual Basic .NET.

We can safely say that For Next loop is the simplest loop to understand and it's certainly the most common type of looping statement in Visual Basic.

Its syntax is as follows:

    For variable [As data_type ] = start_value To stop_value [ Step increment ]
        statements
    [Exit For]
        statements
    [Continue For]
        statements
    Next [ variable ]
The variable value is the looping variable that controls the loop so when the app reaches the For statement, it sets variable equal to start_value . Then it compares variable to stop_value and if variable has passed stop_value, the loop exits.

    For i As Integer = 0 To 10 Step 1
        Console.WriteLine(i)
    Next i

Visual Basic doesn't require the variable's name in the Next statement, but it makes the code easier to read

Note: the loop may not execute even once depending on the start and stop values.

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.