Arrays and For … Each Loops

If you think way back to when we did different types of looping structures, one of the methods to loop was using the For loop. The purpose of the for each loop was that the loop would manage counting for us. It turns out that since an array is a collection of variables held in a common structure, you can use a for loop with it. This type of loop, usually called a For … Each loop, is used when you have a collection of things and you wanted to iterate through each one of them, one at a time. Since an array is a collection of variables, the For … Each loop takes one element out of the array at a time and lets you do something with it. The loop will continue until it has gone through all the elements in the array. The For … Each loop does not need an iterator variable, since the loop manages that counting for you.

From the previous example of summing up all the values in an array, a For Each loop would look like the following:

Code for Using a For … Each loop with an Array

Example Output

Code example output