VB.Net Tutorial/Statements/Do While
Do While Loop
<source lang="vbnet">Option Strict On
Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do While counterVariable < 10 Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop End Sub End Module</source>
counterVariable: 0 counterVariable: 1 counterVariable: 2 counterVariable: 3 counterVariable: 4 counterVariable: 5 counterVariable: 6 counterVariable: 7 counterVariable: 8 counterVariable: 9
Do While/Loop: multiplies and displays product while product is less than or equal to 1000
<source lang="vbnet">Module Tester
Sub Main() Dim product As Integer = 2 Do While product <= 1000 Console.Write("{0} ", product) product = product * 2 Loop Console.WriteLine("Smallest power of 2 " & _ "greater than 1000 is {0}", product) End Sub
End Module</source>
2 4 8 16 32 64 128 256 512 Smallest power of 2 greater than 1000 is 1024
Exit Do While
<source lang="vbnet">public class Test
public Shared Sub Main Dim j As Integer For i As Integer = 1 To 3 j = 0 Do While j < 3 j += 1 For k As Integer = 1 To 3 Dim test1 As Boolean = k = 2 If test1 Then Exit For " Exits the For K loop. Dim test2 As Boolean = i = j If test2 Then Exit Do " Exits the Do. Console.WriteLine(i & ", " & j & ", " & k) Next k Loop Next i End Sub
End class</source>
2, 1, 1 3, 1, 1 3, 2, 1