Many times throughout this course we have written while loops of the following format:
int i = 0;
while (i < N) {
// body
i = i + 1;
}
Sometimes, for greater conciseness, and to prevent forgetting to increment the counter,
Java programmers will instead write this loop as follows, using the for keyword:
for (int i = 0; i < N; i = i + 1) {
// body
}
For this course, I will not use this notation because the three statements inside the for
are executed at three separate locations in the code, so the for loop obscures code clarity slightly.
If you see this loop elsewhere though, its meaning is identical to the while loop above.
And you may feel free to use this loop if it's something to which you're already accustomed.