June 2025
Intermediate to advanced
1129 pages
53h
English
for loops often traverse arrays or data structures. As earlier in Section 4.1.6 , when calculating the mean value, we can use the following code:
double sum = 0;for ( int i = 0; i < numbers.length; i++ ) sum += numbers[ i ];double arg = sum / numbers.length;
The only justification for the loop variable i is to serve as an index; only with i can the element be addressed at a certain place in the array.
Because complete iterations of arrays are frequent, the following shortcut can be used for such iterations:
for ( type identifier: Array ) …
The extended form of the for loop detaches from the index and queries each element of the array. Think of this as passing through a set because the colon can be read ...
Read now
Unlock full access