Menu

Java for Loop: Syntax, Examples, and the Enhanced for-each

The Java for loop explained - the classic three-part loop, looping over arrays and lists, nested loops, break and continue, and the enhanced for-each.

This page includes runnable editors - edit, run, and see output instantly.

The Classic Three-Part for Loop

A for loop repeats a block a controlled number of times. Java's form packs three things into the parentheses, separated by semicolons:

The three parts run in a fixed rhythm:

  1. Initialization (int i = 0) - runs once, before anything else.
  2. Condition (i < 5) - checked before each pass; the loop stops when it is false.
  3. Update (i++) - runs after each pass, before the condition is checked again.

So this prints i = 0 through i = 4. The counter i is scoped to the loop - it does not exist after the closing brace.

Counting Up, Down, and By Steps

The three parts give you full control over the counter:

i-- decrements, i += 5 jumps in steps of five - both are arithmetic operators doing the counter math. The condition decides direction-aware stopping: use > when counting down, < or <= when counting up.

Looping Over an Array

A counted loop pairs naturally with an array, using .length as the bound and i as the index:

Note colors.length is a field with no parentheses - arrays are special that way. A List instead uses list.size(). The condition i < colors.length is correct: indices run from 0 to length - 1, so using < (not <=) avoids an out-of-bounds error.

The Enhanced for-each Loop

When you just need each element and never the index, the enhanced for is cleaner and harder to get wrong - there is no counter to off-by-one:

Read for (String color : colors) as "for each color in colors". It works on arrays and anything iterable. The trade-off: you have no index, and you cannot add to or remove from the collection while looping it (that throws a runtime exception, ConcurrentModificationException). Reach for the counted loop when you need either.

Nested Loops

A loop inside a loop - useful for grids, tables, and pairings. The inner loop runs fully for each pass of the outer:

This prints a 3x3 multiplication grid. Give the counters meaningful names (row, col) rather than i, j once nesting gets deep - it saves you from confusing which is which.

break and continue

break exits the loop immediately. continue skips the rest of the current pass and jumps to the update step:

This prints 1 3 5 7. continue skips the evens; break ends the loop once i would exceed 7. In nested loops, both affect only the innermost loop unless you use a labeled break.

Watch Out for Infinite Loops

If the condition never becomes false, the loop runs forever. The usual cause is forgetting the update or moving the counter the wrong way:

for (int i = 0; i < 5; i--) {   // i only ever decreases -> never reaches 5
    System.out.println(i);
}

An intentional infinite loop omits all three parts - for (;;) { ... } - and relies on a break inside to stop. Use that sparingly; a while (true) usually reads more clearly.

Next: while Loops

The for loop shines when you know the counter up front. When you want to loop until some condition changes - and you do not have a clean counter - the while loop fits better. That is the next page.

Frequently Asked Questions

What is the syntax of a for loop in Java?

A Java for loop has three parts in parentheses separated by semicolons: for (initialization; condition; update) { ... }. For example for (int i = 0; i < 5; i++) { ... } starts i at 0, runs the body while i < 5, and adds 1 to i after each pass. All three parts are optional.

What is the difference between a for loop and a for-each loop in Java?

The classic for loop uses an explicit counter, so you control the index and direction - best when you need the index or want to skip elements. The enhanced for-each loop, for (Type item : collection), walks every element in order without an index - cleaner when you just need each element and never the position. The for-each cannot modify the collection's structure while looping.

How do you loop through an array in Java?

Either index it with a counted loop - for (int i = 0; i < arr.length; i++) { use arr[i]; } - or, when you do not need the index, use a for-each: for (int x : arr) { use x; }. Arrays expose their size via the .length field (no parentheses), while lists use .size().

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED