Programming Tutorials

Do Loop in VB.net

By: Ramlak in VB.net Tutorials on 2008-11-25  

The Do loop keeps executing its enclosed statements while or until (depending on which keyword you use, While or Until) condition is true. You can also terminate a Do loop at any time with an Exit Do statement. The Do loop has two versions; you can either evaluate a condition at the beginning:

Do [{While | Until} condition ]
    [statements]
    [Exit Do]
    [statements]
Loop

or at the end:

Do
    [statements]
    [Exit Do]
    [statements]
Loop [{While | Until} condition]

Here's an example where the code keeps displaying the message "What should I do?" until the user types "Stop" (note that I'm using UCase to uppercase what the user types and comparing it to "STOP" to let them use any combination of case when they type "Stop"):

Module Module1
    Sub Main()
        Dim strInput As String
        Do Until UCase(strInput) = "STOP"
            System.Console.WriteLine("What should I do?")
            strInput = System.Console.ReadLine()
        Loop
    End Sub
End Module
Tip 

The second form of the Do loop insures that the body of the loop is executed at least once.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)