Q1. What is a for loop in Java and when is it used ?
A for loop in Java is a control flow statement used to execute a block of code a fixed number of times. It is preferred when the number of iterations is known before the loop starts.
A for loop consists of three parts:
Example: Printing numbers from 1 to 5
Output
Use a for loop when:
A for loop consists of three parts:
- Initialization – executed once at the beginning
- Condition – checked before every iteration
- Update – executed after each iteration
Example: Printing numbers from 1 to 5
for (int i = 1; i <= 5; i++) { System.out.println(i);}Output
1
2
3
4
5Use a for loop when:
- The number of iterations is predetermined
- Iterating through arrays using indexes
- Writing counters, patterns, or fixed-range loops
Q2. What is an enhanced for loop in Java and why is it used ?
The enhanced for loop (also known as the for-each loop) is used to iterate over arrays or collections without using an index variable.
It improves
Example: Iterating over an array
Output
Use an enhanced for loop when
It improves
- Code readability
- Simplicity
- Safety (avoids index-related errors)
Example: Iterating over an array
String[] fruits = {"Apple", "Banana", "Mango"};
for (String fruit : fruits) { System.out.println(fruit);}Output
Apple
Banana
MangoUse an enhanced for loop when
- You want to access elements directly
- Index values are not required
- Working with arrays or collections
Q3. How do you write an infinite for loop in Java ?
An infinite for loop is created by omitting the condition section. Since no condition stops the loop, it runs indefinitely.
Example: Infinite loop
Output
Use infinite loops:
Example: Infinite loop
for (;;) { System.out.println("Running..."); break; // prevents actual infinite execution}Output
Running...Use infinite loops:
- In servers
- Event listeners
- Game loops
Q4. What is a labeled for loop in Java and when should it be used ?
A labeled for loop allows you to break or continue an outer loop from within a nested loop.
This is useful when working with deeply nested loops and you want precise control over loop termination.
Example: Breaking an outer loop
Output
Use labeled loops:
This is useful when working with deeply nested loops and you want precise control over loop termination.
Example: Breaking an outer loop
outer:for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) break outer; System.out.println(i + " " + j); }}Output
1 1
1 2
1 3
2 1Use labeled loops:
- Only when nested loops require controlled exits
- Avoid overuse as it can reduce readability
Q5. How can a for loop be optimized for better performance ?
To improve performance, avoid repeated calculations inside the loop condition, such as repeatedly calling array.length.
Optimized Example:
Output:
Optimization is important when
Optimized Example:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, n = arr.length; i < n; i++) { System.out.println(arr[i]);}Output:
1
2
3
4
5Optimization is important when
- Working with large datasets
- Writing performance-critical code
Q6. How does a nested for loop work in Java ?
A nested for loop means placing one for loop inside another.
The inner loop completes all its iterations for each iteration of the outer loop.
Example: Printing a 3×3 matrix
Output
Nested loops are used for:
The inner loop completes all its iterations for each iteration of the outer loop.
Example: Printing a 3×3 matrix
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print(j + " "); } System.out.println();}Output
1 2 3
1 2 3
1 2 3Nested loops are used for:
- 2D arrays
- Matrix operations
- Pattern printing
Q7. Can a for loop have no body in Java ?
Yes, a for loop can have an empty body if all the logic is placed in the loop’s header (initialization, condition, or update).
Example: Printing numbers without a loop body
Output
Note
Empty-body loops are rarely used and should be written carefully to maintain readability.
Example: Printing numbers without a loop body
for (int i = 1; i <= 5; System.out.println(i++));Output
1
2
3
4
5Note
Empty-body loops are rarely used and should be written carefully to maintain readability.
