Do Loop in VB.net
By Ramlak Viewed: 31739 times Emailed: 189 times Printed: 181 times
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. |
Comments(0)
Be the first one to add a comment
Latest Tutorials
| [2009-03-22] | Handling Timer Events - and Creating an Alarm Clock in VB.net |
| [2009-03-22] | Creating Menus in Code using VB.net |
| [2009-03-22] | Creating Context Menus in Code using VB.net |
| [2009-03-22] | Creating Tree Views in Code using VB.net |
| [2009-03-22] | Creating List Views in Code using VB.net |
| [2009-03-16] | Send SMS using VB code |
| [2009-02-27] | Creating a Windows Service in VB.net |
| [2009-02-27] | Creating a Windows Service Installer in VB.net |
| [2009-02-24] | OleDbConnection class in VB.net |
| [2009-02-24] | OleDbDataAdapter class in VB.net |
| [2009-02-24] | DataSet Class in VB.net |
| [2009-02-24] | DataTable Class in VB.net |
| [2009-02-24] | DataRow Class in VB.net |
| [2009-02-22] | Chat Server in VB.net |
| [2009-02-22] | A tutorial on Chat Server and Chat Client in VB.net |
Most Viewed Articles (in last 30 days)

