Using For Loop in Java

To repeat a block of code in Java, use a for loop. A for loop has an initialization that runs once, a condition that is checked before each iteration and an update that runs after it. The loop ends when the condition is false. In this Java For Loop Example, we print the numbers from 1 to 5 with a for loop. Click Execute to run the Java For Loop Example online and see the result.
Using For Loop in Java Execute
public class ForLoopExample {
    public static void main(String[] args) {
        for(int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}

Java for Loop Syntax

A for loop has three parts in parentheses, separated by semicolons: initialization, condition and update.

Java for Loop Syntax
for (initialization; condition; update) {
    // statements
}

The initialization runs once, before the loop starts. The condition is checked before each iteration, and the loop ends when it is false. The update runs after each iteration and usually increments or decrements a counter. All three parts are optional: for (;;) loops forever.

Java For Loop Examples

The following are examples of the for loop in Java. Run them in the online Java code runner to see each iteration printed.

Count down with a for loop

The update can decrement the counter as well as increment it.

Java for Loop Countdown Example
public class CountdownExample {
    public static void main(String[] args) {
        for (int i = 3; i > 0; i--) {
            System.out.println(i);
        }
        System.out.println("Go");
    }
}

// output:
// 3
// 2
// 1
// Go

Loop through an array

Array indexes run from 0 to length - 1, so the condition is i < array.length.

Java for Loop Array Example
public class ArrayLoopExample {
    public static void main(String[] args) {
        int[] prices = {10, 20, 30};
        int total = 0;
        for (int i = 0; i < prices.length; i++) {
            total += prices[i];
        }
        System.out.println("Total: " + total);
    }
}

// output: Total: 60

Loop through a List with the enhanced for loop

The enhanced for loop, also called for-each, goes through every element of an array or a collection such as a List, without an index variable.

Java Enhanced for Loop Example
import java.util.List;

public class EnhancedForExample {
    public static void main(String[] args) {
        List<String> languages = List.of("PHP", "Python", "Java");
        for (String language : languages) {
            System.out.println(language);
        }
    }
}

// output:
// PHP
// Python
// Java

Loop through the characters of a String

To read a String character by character, loop from 0 to length() - 1 and call charAt().

Java for Loop String Example
public class StringCharsExample {
    public static void main(String[] args) {
        String word = "Java";
        for (int i = 0; i < word.length(); i++) {
            System.out.println(i + ": " + word.charAt(i));
        }
    }
}

// output:
// 0: J
// 1: a
// 2: v
// 3: a

See also

Updated: