In previous examples, we demonstrated how to use counter-controlled
for statements to iterate through the elements in an array. In this section, we introduce the foreach statement, which iterates through the elements of an entire array or collection. This section discusses how to use the
foreach statement to loop through an array. We show how to use it with collections in Chapter 21. The syntax of a
foreach statement is:
foreach ( type identifier in arrayName ) statement
where type and identifier are the type and name (e.g.,
int number) of the iteration variable, and arrayName is the array through which to iterate. The type of the iteration variable must be consistent with the type of the elements in the array. As the ...