Java Notes
Complete guide to Java for loops — traditional for loop, enhanced for-each loop, infinite loops, multiple variables, nested loops, and practical patterns with examples.
The for loop is Java's most versatile looping construct. Use it when you know in advance how many times you want to iterate, or when iterating over collections and arrays.
Traditional For Loop
Count up: 1 2 3 4 5 Count down: 10 9 8 7 6 5 4 3 2 1 Even numbers: 2 4 6 8 10 12 14 16 18 20 Two variables: i=0, j=10 i=1, j=9 i=2, j=8 i=3, j=7 i=4, j=6
Anatomy of a For Loop
Execution order: Body: i = 0 Body: i = 1 Body: i = 2
Enhanced For Loop (For-Each)
public class ForEachLoop {
public static void main(String[] args) {
// For arrays
int[] numbers = {10, 20, 30, 40, 50};
System.out.print("Array: ");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
// For strings (via toCharArray)
String word = "Java";
System.out.print("Chars: ");
for (char c : word.toCharArray()) {
System.out.print(c + " ");
}
System.out.println();
// For collections
java.util.List<String> fruits = java.util.List.of("Apple", "Banana", "Cherry");
System.out.println("Fruits:");
for (String fruit : fruits) {
System.out.println(" - " + fruit);
}
// LIMITATION: can't modify elements or access index
// For index-based operations, use traditional for loop
}
}Array: 10 20 30 40 50 Chars: J a v a Fruits: - Apple - Banana - Cherry
Practical Examples
Multiplication Table
Multiplication table for 7: 7 × 1 = 7 7 × 2 = 14 7 × 3 = 21 7 × 4 = 28 7 × 5 = 35 7 × 6 = 42 7 × 7 = 49 7 × 8 = 56 7 × 9 = 63 7 × 10 = 70
Factorial Calculation
10! = 3628800 Factorial table: 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5,040 8! = 40,320 9! = 362,880 10! = 3,628,800
Prime Number Check
29 is prime Primes up to 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Infinite For Loop
1 2 3 4 5 Loop ended with break
Common Mistakes
- Off-by-one errors —
for(i=0; i<=n; i++)runs n+1 times,for(i=0; i<n; i++)runs n times. Think carefully about<vs<=. - Modifying loop variable inside body —
for(int i=0; i<10; i++) { i++; }skips iterations. Avoid modifying the counter inside the loop. - Infinite loop by accident —
for(int i=0; i<10; i--)decrements instead of increments — never terminates. - Using for-each when index is needed — for-each doesn't provide an index. If you need
i, use traditional for. - Scope of loop variable —
for(int i=0;...)—iis not accessible outside the loop.
Interview Questions
Q1: What are the three parts of a for loop? Are they required?
Answer: Initialization (runs once), condition (checked before each iteration), and update (runs after each iteration). All three are optional. for(;;) creates an infinite loop. You can omit any part: for(;condition;) is like a while loop.
Q2: What is the difference between for and for-each loops?
Answer: Traditional for gives you an index variable, allows forward/backward/skip iteration, and lets you modify the array. Enhanced for-each provides simpler syntax for sequential iteration but doesn't give index access and doesn't allow modification of the collection during iteration.
Q3: How do you iterate an array in reverse with a for loop?
Answer: for (int i = array.length - 1; i >= 0; i--) starts at the last index and decrements. This isn't possible with for-each, which always goes forward.
Q4: Can you declare multiple variables in a for loop?
Answer: Yes, but they must be the same type: for (int i = 0, j = 10; i < j; i++, j--). You cannot declare different types (use separate declarations before the loop).
Q5: When should you use for vs while?
Answer: Use for when you know the iteration count or have a clear counter pattern. Use while when the termination condition depends on something other than a counter (user input, file reading, condition becoming true). For-each is best for iterating all elements of a collection sequentially.
Summary
The for loop is ideal for count-controlled iteration. The traditional form gives full control over initialization, condition, and update. The enhanced for-each simplifies array/collection iteration. Choose the right variant: traditional for when you need an index or custom stepping, for-each when you just need each element sequentially.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for For Loop in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, fundamentals, control, statements
Related Java Master Course Topics