Lesson 11 / 38
Loops
Compare Java's for, while, do-while and enhanced for-each loops, and control iteration with break, continue and labelled statements.
The four forms
for when you know the count, while when you loop until a condition, do-while to run the body at least once, and the enhanced for (for-each) to walk any array or Iterable.
int[] nums = {2, 4, 6};
int sum = 0;
for (int n : nums) sum += n; // for-each
int i = 0;
while (i < 3) { System.out.print(i); i++; }break, continue, labels
break exits the innermost loop; continue skips to the next iteration. A labelled break outer; can jump out of nested loops — use sparingly.