There are two Do Loops in Visual Basic: the Do While and Do Until. The Do While loops something which is true, and the Do Until loops until a certain condition is met. Create a new VB Console Application and name it Do Loops.
Dim myNumber As Integer = 0 Do Until myNumber = 5 Console.WriteLine("Pick a number between 1 and 6") myNumber = Console.ReadLine() If myNumber = 5 Then Console.WriteLine("Yes 5, it is!") Else Console.WriteLine("Oops that's not the lucky number!") End If Loop Console.ReadLine()
- First we make a integer and call it myNumber, and set its default value to 0
- Then we have a Do Until Statement which loops until myNumber = 5
- Next we ask the user to pick a number between 1 and 6
- We read the user's value and store it in myNumber
- If the number is not 5 then they get the text "Oops that's not the lucky number"
- If they do not pick 5 then the question will be asked again. (It is an infinite loop, so it will keep looping until myNumber is 5)
- If they pick 5 then they will get the message "Yes 5, it is!" The program will close after this.
The Do While is similar to the Do Until but it evaluates something which is true.
Dim mySecondNumber As Integer Do While mySecondNumber < 50 mySecondNumber += 1 Console.WriteLine(mySecondNumber) Loop Console.ReadLine()
- First we make a integer called mysecondNumber
- We use the Do While loop and see if mysecondNumber is less than 50 (which is true)
- We then increment it by 1 until it reaches 50 (then the Do While will become false)
You can use the following operators with Do Until and Do While:
|<=||Less than or equal to|
|>=||Greater than or equal to|
|<>||Not equal to|
Loop While and Loop Until
It is possible to place the While and Until after the loop so it appears as:
Dim myNumber As Integer = 0 Do myNumber += 1 Console.WriteLine(myNumber) Loop While myNumber < 10 Do myNumber += 1 Console.WriteLine(myNumber) Loop Until myNumber = 10 Console.ReadLine()
In this example, the Until and While are after the loop, and the code will always execute at least once. Now if you run this code, it will crash and give an overflowException: the first Loop While will execute and it will keep going until myNumber is 10, and then it will exit. The Do Until loop will then execute, and this will keep looping (infinitely) because myNumber is no longer equal to 10. The Do Loop executed once and myNumber += 1 changed its value to 11, and therefore this loop will execute until it reaches 10, but it won't!
It is best to use Do While and Do Until instead of putting the While and Until after the loop, because they will always keep executing. The Do While/Until will not execute if its condition is false. Issues like the above example cause bugs in your application since the variable changed.
Dim myNumber As Integer = 0 Do myNumber += 1 Console.WriteLine(myNumber) Loop While myNumber < 10 Do Until myNumber = 10 myNumber += 1 Console.WriteLine(myNumber) Loop Console.ReadLine()
This is similar to the above example, however this time when the first loop executes and reaches 10, the second loop will not execute because myNumber is already 10.
This tutorial covered the different types of Do Loops and also the operators. In addition you learned about Loop Until and Loop While.