Notes / Java / Loops

Loops in Java — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Loops Java offers the same loop types as C/C++ — for, while, do-while — plus an enhanced for-each loop for iterating over arrays and collections cleanly..

Syntax

syntax
for (init; condition; update) { }
for (type item : array) { }  // for-each

Example

java
public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        for (int n : nums) {
            System.out.print(n + " ");
        }
    }
}

Output

output1 2 3 4 5

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview Know when for-each isn't enough — e.g., you can't remove elements from a list safely while for-each iterating it; use an Iterator instead.
Back to notes Read full lesson →