VB.Net Tutorial/Statements/Do While

Материал из VB Эксперт
Перейти к: навигация, поиск

Do While Loop

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
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

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
2  4  8  16  32  64  128  256  512  Smallest power of 2 greater than 1000 is 1024

Exit Do While

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
2, 1, 1
3, 1, 1
3, 2, 1