for Loop
The for loop in Java is a control flow statement used to execute a block of code repeatedly for a fixed number of iterations. It is most commonly used when the number of iterations is known in advance. This is a fundamental concept for logic building, algorithms, and interviews.
What Is a for Loop?
- Repeats a block of code
- Combines initialization, condition, and update in one line
- Best suited for count-controlled loops
Basic Syntax
for (initialization; condition; update) {
// code to execute
}
Breakdown of for Loop Components
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
1. Initialization
- Executed once at the start
- Used to initialize loop variable
int i = 1;
2. Condition
- Evaluated before every iteration
- Loop continues while condition is true
i <= 5
3. Update (Increment / Decrement)
- Executed after each iteration
- Updates loop variable
i++
Execution Flow of for Loop
- Initialization
- Condition check
- Loop body execution
- Update
- Repeat steps 2–4 until condition becomes false
Simple Example
for (int i = 1; i <= 3; i++) {
System.out.println("Java");
}
Output:
Java
Java
Java
for Loop with Decrement
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
Multiple Initialization & Update
for (int i = 1, j = 5; i <= j; i++, j--) {
System.out.println(i + " " + j);
}
Infinite for Loop
for (;;) {
System.out.println("Infinite loop");
}
Why it matters: All three parts are optional, but semicolons are mandatory.
for Loop with break and continue
Using break
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Using continue
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Nested for Loop
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println(i + "," + j);
}
}
Use case: Matrix operations, patterns, tables.
Common Use Cases
- Iterating fixed ranges
- Processing arrays using index
- Generating patterns
- Looping counters
Common Beginner Mistakes
- Off-by-one errors (< vs <=)
- Infinite loops due to wrong condition
- Modifying loop variable inside loop body
- Overcomplicating loop conditions
Interview-Ready Answers
Short Answer
The for loop is used to execute a block of code repeatedly when the number of iterations is known.
Detailed Answer
In Java, the for loop consists of initialization, condition, and update expressions. It evaluates the condition before each iteration and executes the loop body until the condition becomes false, making it ideal for count-controlled iterations.
for Loop Examples (1 to 20)
1. Basic for Loop (Print 1 to 5)
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Explanation
- Initializes i to 1.
- Loop runs while i <= 5.
- Increments i by 1 each iteration.
Output: 1 2 3 4 5
2. for Loop with Custom Increment
for (int i = 0; i <= 10; i += 2) {
System.out.println(i);
}
Explanation
- i increases by 2 each time.
- Prints even numbers only.
Output: 0 2 4 6 8 10
3. Reverse for Loop
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
Explanation
- Loop runs backward.
- Decrements i each iteration.
Output: 5 4 3 2 1
4. for Loop without Initialization
int i = 1;
for (; i <= 3; i++) {
System.out.println(i);
}
Explanation
- Initialization happens outside the loop.
- Valid Java syntax.
Output: 1 2 3
5. for Loop without Increment
for (int i = 1; i <= 3;) {
System.out.println(i);
i++;
}
Explanation
- Increment done inside the loop body.
- Useful when increment depends on logic.
6. Infinite for Loop
for (;;) {
System.out.println("Running once");
break;
}
Explanation
- All three parts are optional.
- Acts as an infinite loop.
- break is required to stop execution.
7. for Loop with break
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Explanation
- Loop terminates when i == 3.
Output: 1 2
8. for Loop with continue
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Explanation
- Skips only iteration i == 3.
Output: 1 2 4 5
9. for Loop with Multiple Variables
for (int i = 1, j = 5; i <= 5; i++, j--) {
System.out.println(i + " " + j);
}
Explanation
- Two variables updated in one loop.
Output:
1 5
2 4
3 3
4 2
5 1
10. Nested for Loop (Matrix Style)
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(j + " ");
}
System.out.println();
}
Explanation
- Inner loop runs fully for each outer loop.
Output:
1 2 3
1 2 3
1 2 3
11. Nested for Loop (Multiplication Table)
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i + " * " + j + " = " + (i * j));
}
}
Explanation
- Used for tables and grid processing.
12. Enhanced for Loop (Array Traversal)
int[] nums = {10, 20, 30};
for (int n : nums) {
System.out.println(n);
}
Explanation
- Simplifies array iteration.
- No index required.
13. for Loop with Array Index
int[] nums = {10, 20, 30};
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
Explanation
- Index-based iteration.
- Needed when index manipulation is required.
14. for Loop with Conditional Logic
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
System.out.println(i + " is even");
}
}
Explanation
- Executes logic only when condition matches.
- Output: even numbers only.
15. Labeled for Loop
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println("i=" + i + ", j=" + j);
}
}
Explanation
- continue outer jumps to next outer loop iteration.
- Used in complex nested logic.
16. for Loop for String Characters
String s = "JAVA";
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
Explanation
- Iterates character by character.
- Common in string processing.
17. for Loop for Sum Calculation
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println(sum);
Explanation
- Accumulates values.
- Output: 15
18. for Loop for Searching an Element
int[] nums = {5, 10, 15, 20};
for (int n : nums) {
if (n == 15) {
System.out.println("Found");
break;
}
}
Explanation
- Stops loop once element is found.
- Improves performance.
19. for Loop with Boolean Flag
boolean found = false;
for (int i = 1; i <= 5; i++) {
if (i == 4) {
found = true;
break;
}
}
System.out.println(found);
Explanation
- Flag stores result after loop.
- Common interview pattern.
20. Interview Summary Example
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
Explanation
- Demonstrates:
- Initialization
- Condition
- Increment
- Most fundamental for loop structure.
Key Takeaway
The for loop provides compact, readable, and controlled iteration. Mastering its flow and edge cases is essential for writing efficient Java logic.