Quiz: Enhanced For Loop
Which of these loops can be easily rewritten as an enhanced for loop?
-
Yes|No
for (i = 0; i < values.length; i++) { if (values[i] == 0) { count++; } } -
Yes|No
int sum = 0; for (i = 1; i < values.length; i++) { if (values[i - 1] > values[i]) { sum = sum + values[i] } }It possible to write this as an enhancedforloop (try it—you need another variable to remember the previous value), but Sara says it isn't easy. If it's easy for you, great! - Yes|No
for (i = 0; i < values.length; i++) { values[i] = i * i; }You can not modify array values in an enhancedforloop.