Codectionary / Developer documentation / Java

for Loop

The for loop repeats a block of code a specific number of times, controlled by three parts in its header: an initialization (run once), a condition (checked before every iteration), and an update (run after every iteration). Java also offers an enhanced for-each loop for cleanly iterating over arrays and collections without manually managing an index.

Syntax

for (initialization; condition; update) {\n  // code\n}

Examples

Basic for Loop

The classic counting loop with initialization, condition, and update.

for (int i = 0; i < 5; i++) {
    System.out.println(i);  // 0, 1, 2, 3, 4
}

Enhanced for-each Loop

A simpler syntax for iterating over every element of an array or collection.

int[] numbers = {10, 20, 30, 40};

for (int num : numbers) {
    System.out.println(num);
}

String[] names = {"Fola", "Zain", "Jamal"};
for (String name : names) {
    System.out.println("Hello, " + name);
}

Nested for Loops

A loop inside another loop, commonly used for working with grids or generating combinations.

for (int row = 1; row <= 3; row++) {
    for (int col = 1; col <= 3; col++) {
        System.out.print(row * col + " ");
    }
    System.out.println();
}

for Loop with Multiple Variables

Java's for loop can initialize and update more than one variable at once.

for (int i = 0, j = 10; i < j; i++, j--) {
    System.out.println("i=" + i + ", j=" + j);
}

Best practices

  • Use the enhanced for-each loop whenever you only need each element, not its index - it is safer and more readable
  • Use a traditional for loop when you need the index, need to iterate backward, or need to skip elements with a custom step
  • Avoid modifying the loop counter variable inside the loop body - it can create subtle, hard-to-follow bugs
  • Prefer for over while when the number of iterations is known in advance, since it groups all loop control in one place

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.
Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.
Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.