The Do Loop

There are certain cases in which you need to execute the body of the loop while a specific expression is True (or False). This means executing a loop an unknown number of times. You would not be able to use a counter and a range of values, as in a For loop. You can use a Do loop to solve this type of problem.

In the following example you would like to allow the user to perform a simple mathematical computation as many times as he or she wishes. To run the example, create a new console project and enter the code as shown.

Sub Main()
   Dim sContinue As String = "yes"

   Do While sContinue.Equals("yes") Console.Out.WriteLine("Enter the value for i and press <Enter>:") Dim i As Integer i = CInt(System.Console.In.ReadLine()) Console.Out.WriteLine("Enter ...

Get Visual Basic® .NET by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.