VB.NET Do While Loop Examples (While) - Dot Net Perls
Có thể bạn quan tâm
Suppose in VB.NET we wish to continue looping until a condition is met. The number of iterations though may be unknown (as of yet).
With Do While, and While, we can loop indefinitely (and stop at a certain point). These loops are ways to continue iterating while one or more conditions are true.
Example
A Do While loop can have one or more conditions in its expression. Here we use 2 conditions. We continue iterating while "i" is positive and "z" is less than or equal to 8.
Then In each iteration, we change the values of these variables: "i" is decremented by 10 and "z" is incremented by 3.Finally The loop ends because "z" exceeds 8. It is changed to 9 in the final iteration and then the loop terminates.Also If the "z" condition in the loop expression was removed, the loop would continue until "i" was set to -10.Do loop
With Do we can loop infinitely (or indefinitely). This loop gets a random number on each iteration. If the number is even, it uses "Exit Do" to stop the loop.
However This style of loop can be used to run until a certain condition is met. This can even be an infinite loop.Tip We can avoid the "While true" condition for an infinite loop, and just use a Do Loop.Module Module1 Sub Main() Dim random As New Random() ' Enter a loop. Do ' Get random number. Dim number As Integer = random.Next ' Print random number. Console.WriteLine(number) ' Exit loop if we have an even number. If number Mod 2 = 0 Then Exit Do End If Loop End Sub End Module1315809053 1322882256While versus Until
In VB.NET we can use a Do Until loop to mean "continue until the condition is matched." A Do While loop can be used in the same way.
Here We use the "not equals" operator with a Do While loop to continue until an array element is equal to 30.And We show a Do Until loop that has the same effect as the Do While loop. Use whichever is clearest in the program.Module Module1 Sub Main() Dim index As Integer = 0 Dim array() As Integer = New Integer() {10, 20, 30, 40} ' Use "not equals" operator. Do While array(index) <> 30 Console.WriteLine("[WHILE] NOT 30: {0}", array(index)) index += 1 Loop ' Use "do until" loop. index = 0 Do Until array(index) = 30 Console.WriteLine("[UNTIL] NOT 30: {0}", array(index)) index += 1 Loop End Sub End Module[WHILE] NOT 30: 10 [WHILE] NOT 30: 20 [UNTIL] NOT 30: 10 [UNTIL] NOT 30: 20While example
Next, we can use a "While" loop without the "Do" keyword. While-loops are useful for cases where we do not know beforehand how many iterations will run.
Detail The two statements in the While-loop's body are executed repeatedly until that condition evaluates to false.Tip You can also use the "And" operator to put 2 conditions in the While-loop.Note This will result in a more complex program, but sometimes multiple conditions are necessary.Module Module1 Sub Main() Dim i As Integer = 0 While i < 100 Console.WriteLine(i) i += 10 End While End Sub End Module0 10 20 30 40 50 60 70 80 90Do While in VB.NET is a loop that will continue until the exit condition is met. This can lead to infinite loops unless you are careful.
Từ khóa » Visual Basic While Vs Do While
-
Visual Basic (VB) Do While Loop - Tutlane
-
Whats The Difference Between Do While And While In VB.NET?
-
Do...Loop Statement - Visual Basic - Microsoft Docs
-
While Loop, Do While Loop, Do Until Loop, While End While - VB.NET
-
Vòng Lặp Do While Trong Visual Basic (VB)
-
Visual Basic Tutorial - 31 - Do While Loop - YouTube
-
Do While Statement In Visual Basic?
-
Visual Basic Do Loops | The Coding Guys
-
VBA - Do-While Loops - Tutorialspoint
-
VB.NET Do Loop - Javatpoint
-
Thực Hành Với Visual Basic: Cấu Trúc Lặp Do ... Loop While
-
Thread: "Do Until" Vs "Do While" - VBForums
-
Excel VBA Do While Loop And (Do Loop While) - A Guide
-
Visual Basic Do ... Loops