2 · Control Flow & Methods · lesson 9 of 41

Loops

for, while, do-while, and the for-each loop.

Java
for (int i = 0; i < 10; i++) { ... }

while (queue.hasNext()) { ... }

do { retry(); } while (!ok);

for (String name : names) {          // for-each
    System.out.println(name);
}
NOTE
Use break to exit a loop early; continue skips to the next iteration.
Key takeaways
  • ✓for-each is the cleanest iteration over any Iterable.
  • ✓Prefer streams over manual loops for data transformations.