Tuesday, February 7, 2012

Challenge 044’s Solution

The code in Challenge 44 can be rewritten using a for loop in the following way:
 
        int sum = 0;
        for (int index = 0; index < arr.length; index++)
        {
            sum += arr[index];
        }

It can also be rewritten using a for-each loop in the following way:
.
        int sum = 0;
        for (int index : arr)
        {
            sum += index;
        }
 
−Phoenix

1 comment: