Exit Do Statement in Visual Basic .NET

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

This lesson describes the quitting Do loops in Visual Basic .NET

The Exit Do statement allows the program to leave the nearest enclosing loop before it would normally finish.

Meaning, it lets you end a loop early and can be used e.g. when testing an endless loop.

Its syntax is as follows:

    Do {While} condition
    [Exit Do]

In the following example, condition stops the loop if the counter is equal to zero. The If statement in the loop, however, causes the Exit Do statement to stops the loop when the array element value is 10:

    Dim array() As Integer = {1, 3, 5, 7, 10, 20, 50}
    Dim counter As Integer = array.GetUpperBound(0) + 1
    Do While counter > 0
        ' quit the do loop if element's value is 10
        If array(counter) = 10 Then
            Exit Do
        End If
        counter -= 1
        'print the array elements
    ' this loop prints out only 50 and 20

Previous Lesson | Next Lesson

Share This

Home | About | Contact | Privacy Policy

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