VB.Net Each...Next迴圈

2019-10-16 23:02:09

它為集合中的每個元素重複執行一組語句,可簡單理解為遍歷陣列元素。此迴圈用於存取和運算元組或VB.Net集合中的所有元素。

這個迴圈結構的語法是:

For Each element [ As datatype ] In group
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ element ]

範例

Module loops
   Sub Main()
      Dim anArray() As Integer = {1, 3, 5, 7, 9}
      Dim arrayItem As Integer
     'displaying the values
      For Each arrayItem In anArray
          Console.WriteLine(arrayItem)
      Next
      Console.ReadLine()
   End Sub
End Module

執行上面範例程式碼,得到以下結果 -

1
3
5
7
9