← Back to Core Java Course | Chapter 4: Control Flow | Lesson 7 of 9

Java for Loop

A for loop repeats steps a set number of times, like doing ten jumping jacks while counting. It keeps the start, the stopping rule and the counting all in one line.
Syntax
java
for (initialization; condition; update) {
    // loop body
}

Basic for Loop

A for loop packs initialization, the continue condition, and the update step into a single header line, making it the natural choice whenever you know in advance roughly how many times you need to iterate.

Example: Basic for Loop

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Loop: repeat while the condition holds
		for (int i = 0; i < 3; i++) {
			// Print a line to the console
			System.out.println(i);
		}
	}
}

Decrementing for Loops

Initializing the loop variable at a high value and using i-- as the update step walks the loop backward, which is handy for tasks like processing an array from its last element to its first.

Example: Decrementing for Loops

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Loop: repeat while the condition holds
		for (int i = 5; i > 0; i--) {
			// Print a line to the console
			System.out.println(i);
		}
	}
}

Iterating with Custom Steps

The update expression isn't limited to i++ -- writing i += 2 steps by twos, letting you visit every other index or count by any custom increment your logic needs.

Example: Iterating with Custom Steps

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Loop: repeat while the condition holds
		for (int i = 0; i < 10; i += 2) {
			// Print a line to the console
			System.out.println(i);
		}
	}
}

Iterating over Arrays

Because array indices run from 0 to length-1, a for loop like for (int i = 0; i < arr.length; i++) is the standard, idiomatic way to visit and process every element of an array in order.

Example: Iterating over Arrays

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare arr and set it to {10, 20, 30}
		int[] arr = {10, 20, 30};
		// Loop: repeat while the condition holds
		for (int i = 0; i < arr.length; i++) {
			// Print a line to the console
			System.out.println(arr[i]);
		}
	}
}

Nested for Loops

An inner for loop runs to completion for every single iteration of its enclosing outer loop -- with two loops each running n times, that's n squared total iterations, the classic pattern behind processing 2D grids or comparing all pairs in a list.

Example: Nested for Loops

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Loop: repeat while the condition holds
		for (int i = 0; i < 2; i++) {
			// Loop: repeat while the condition holds
			for (int j = 0; j < 2; j++) {
				// Print a line to the console
				System.out.println(i + "," + j);
			}
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using <= with array.length, which causes ArrayIndexOutOfBoundsException.
  2. Putting a semicolon after the for (...), which makes the loop body separate.
  3. Changing the loop variable inside the body, which changes how many times it runs.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.