Which loop correctly traverses every valid index of a?
for (int i = 0; i < a.length; i++)for (int i = 0; i <= a.length; i++)for (int i = 1; i <= a.length; i++)for (int i = a.length; i > 0; i++)0 and continues while the index is less than array.length.
Explanation
The initial value, the condition, and the update together decide which indexes the loop visits. Starting at 0, stopping before a.length, and increasing by one visits every index from 0 through a.length - 1 exactly once.
- B is not correct: The condition
i <= a.lengthlets the index reacha.lengthitself, which is one position past the last valid index. - C is not correct: Starting at
i = 1skips the first element, andi <= a.lengthalso runs one index past the end. - D is not correct: It begins at
a.length, which is not a valid index, and the updatei++moves the index further away from0instead of toward it.