For Each Iteration Statements

59 For k = 1 To 10 ... Next k, j, I This code is equivalent to the following: For i = 1 To 10 For j = 1 To 10 For k = 1 To 10 ... Next Next Next I recommend the latter style, since it is considered more structured to terminate each block explicitly. It is interesting to note that the For loop is equivalent to the following Do loop construction assuming that step_expression is nonnegative: loop_variable = from_expression Do While loop_variable = to_expression statements loop_variable += step_expression Loop If step_expression is negative, the For loop is equivalent to this only the comparison in the Do statement is different: loop_variable = from_expression Do While loop_variable = to_expression statements loop_variable += step_expression Loop

2.13.3.3 For Each

The For Each statement is similar to the For statement, except that the loop variable need not be numeric, and successive iterations do not increment the loop variable. Instead, the loop variable takes successive values from a collection of values. Here is the syntax: For Each variable In expression statements Next [ variable ] The loop variable can be of any type. The expression must be a reference to an object that exposes the IEnumerable interface interfaces are discussed later in this chapter. Generally, types that are considered collections expose this interface. The .NET Framework class library provides several useful collection types, which are listed in Chapt er 3 . See Sect ion 2.5.4 earlier in this chapter for an explanation of what constitutes a collection type. The type of the items in the collection must be compatible with the type of the loop variable. The statements in the body of the loop execute once for each item in the collection. During each iteration, the loop variable is set equal to each consecutive item in the collection. Because all Visual Basic .NET arrays expose the IEnumerable interface, the For Each statement can be used to iterate through the elements of an array. For example: Dim a As Integer = {1, 2, 3, 4, 5} 60 Dim b As Integer For Each b In a Console.WriteLineb Next This is equivalent to the following code: Dim a As Integer = {1, 2, 3, 4, 5} Dim b As Integer Dim i As Integer For i = a.GetLowerBound0 To a.GetUpperBound0 b = ai Console.WriteLineb Next Because all arrays in Visual Basic .NET implicitly derive from the Array type in the System namespace, the a array in this example has access to methods defined on the Array type specifically GetLowerBound and GetUpperBound. In case youre interested, here is the equivalent code using a Do loop. This is essentially what the For Each statement is doing under the covers, although the For Each construct is likely to compile to faster code. Dim a As Integer = {1, 2, 3, 4, 5} Dim b As Integer Dim e As Object = a.GetEnumerator Do While CTypee.GetType .InvokeMemberMoveNext, _ Reflection.BindingFlags.InvokeMethod, Nothing, e, Nothing, Boolean b = CTypee.GetType .InvokeMemberCurrent, _ Reflection.BindingFlags.GetProperty, Nothing, e, Nothing, Integer Console.WriteLineb Loop

2.13.4 Mathematical Functions