Enhanced for loop

                   Connor Dunnigan

This program demonstrates the use of an enhanced for loop. Traditional for loops look something like: for(int i=0; i<size; i++){sum+=0} . The enhanced for loop looks like: for(int x : arr){sum+=0}

In the function sumListEnhaced(int[] list), the field total is initialized to zero (to count the sum of values in array) and the for loop using the enhanced form.

for(int val : list){

total += val;

}

The first parameter for the for loop is the type and identifier for the values of the array to be stored in as the loop iterates. The second parameter indicates the array to loop through. As the loop iterates, the values of the array are added to the total field which is keeping track of the sum of all of the array values.


public static void addOneError(int[] list)

{   for(int val : list)
    {   val = val + 1;
    }
}

In this for loop, the expression val = val + 1 is really only adding one to that value in the array but does not do anything else. This does not change the contents of the array list nor does it keep a running sum.