VB.Net While... End While迴圈

2019-10-16 23:02:10

只要給定的條件為真(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