Skip to content

Loops

Loops allow a block of code to be executed repeatedly, either a fixed number of times or while a condition holds. They are essential for processing collections, reading streams of data, and implementing repetitive algorithms.

Java provides four loop constructs:

  • for – traditional loop with initialization, condition, and update
  • enhanced for (for‑each) – simplified iteration over arrays and collections
  • while – pre‑test loop (condition checked before each iteration)
  • do‑while – post‑test loop (body executed at least once)

Loop control statements (break, continue, and labels) give fine‑grained control over loop execution (covered in sections 4.3 and 4.4).