traverse the elements in a 1D array object using an enhanced for loop
ENHANCED FOR LOOP
There is a special kind of loop that can be used with arrays that is called an enhanced for loop or a for-each loop.
This loop is much easier to write because it does not involve an index variable or the use of the square brackets [].
It just sets up a variable that is set to each value in the array successively.
To set up a for-each loop, use for(type variable : arrayname) where the type is the type of elements in the array.
See the examples below that loop through an int and a String array.
Notice that the type of the loop variable is the type of the array.
Use the enhanced for each loop with arrays whenever you can, because it cuts down on errors.
You can use it whenever you need to loop through all of the elements of an array and don't need to know their index or need to change their values
It always starts with the first item in the array (the one at index 0) and continues through in order to the last item in the array.
LIMITATIONS
Could an enhanced for loop be used to increment all of the elements in an array?
Unfortunately no!
The variable in the loop is the only thing that changes, not the real array values.
If we want to modify array elements we need an indexed loop like we saw in the previous lesson.
So, in other words, enhanced for loops cannot be used in all situations.
Only use for-each loops when you want to loop through all of the values in an array without changing their values.
Don't use them if you need the index.
Don't use them if you need to change the values in the array.
Don't use them if you want to loop through only part of an array.
Don't use them if you want to loop through an array in a different order.
ENHANCED FOR LOOP ALGORITHMS
Here is an object-oriented example that has the array as a private instance variable in the class and provides a public method named getAverage that uses an enhanced for loop:
SUMMARY
An enhanced for loop, also called a for each loop, can be used to loop through an array without using an index variable.
An enhanced for loop header includes a variable, referred to as the enhanced for loop variable, that holds each value in the array.
For each iteration of the enhanced for loop, the enhanced for loop variable is assigned a copy of an element without using its index.
Assigning a new value to the enhanced for loop variable does not change the value stored in the array.
Program code written using an enhanced for loop to traverse and access elements in an array can be rewritten using an indexed for loop or a while loop.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.
3) Create a "grade average" program! Your program must use an enhanced for loop. You could base your grade program on the grading system used by one of your teachers (weighted, total points, Binary Grading, etc.).