VB.Net While... End While迴圈

只要給定的條件為真(True),它就會執行一系列語句。

這個迴圈結構的語法是:

While condition
    [ statements ]
    [ Continue While ]
    [ statements ]
    [ Exit While ]
    [ statements ]
End While

在這裏,語句(statements)可以是單個或一組語句。 條件(condition)可能是任何運算式,而true表示邏輯為真。當條件成立時,迴圈迭代。

當條件變為false時,程式控制傳遞到迴圈之後的行。

流程圖

在這裏,While迴圈的關鍵是迴圈可能永遠不會運行。當條件被測試並且結果為false時,循環體將被跳過,while迴圈之後的第一條語句將被執行。

示例

Module loops
   Sub Main()
      Dim a As Integer = 10
      ' while loop execution '
      While a < 20
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      End While
      Console.ReadLine()
   End Sub
End Module

執行上面示例代碼,得到以下結果 -

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

上一篇: VB.Net迴圈 下一篇: VB.Net字串