8.7. For Each...Next Repetition Statement

Visual Basic provides the For Each...Next repetition statement for iterating through the values in a data structure, such as an array, without using a loop counter. When used with one-dimensional arrays, For Each...Next behaves like a For...Next statement that iterates through the range of indices from 0 to the value returned by GetUpperBound(0). Instead of a counter, For Each...Next uses a variable to represent the value of each element. The program in Fig. 8.12 uses the For Each...Next statement to determine the minimum value in a one-dimensional array of grades.

Figure 8.12. Using For Each...Next with an array.
					1
					' Fig. 8.12: ForEach.vb
					2
					' Program uses For Each...Next to find the minimum grade.

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.