Example
Let's look at an example of an infinite for loop in Java:
for (int i = 1; i > 0; i++) {
System.out.println("Iteration: " + i);
}
In this example, the initialization part sets the variable i to 1. The condition i > 0 is always true because i is incremented by 1 in each iteration, making it a positive number. As a result, the loop continues to run indefinitely, printing the iteration number.
Here's another example that shows how an infinite for loop can be used with a break statement to exit the loop based on a specific condition:
Java
public class Main {
public static void main(String[] args) {
for (int i = 1; ; i++) {
System.out.println("Iteration: " + i);
if (i == 5) {
break;
}
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this case, the loop runs indefinitely, but the break statement is used to exit the loop when the value of i becomes 5. The output will be:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Explanation
An infinite for loop is a loop that runs continuously without any termination condition. It keeps executing the code block inside the loop indefinitely until a break statement is encountered or the program is manually terminated.
In the first example, the condition i > 0 is always true because i is initialized to 1 and incremented by 1 in each iteration. This means that the loop will continue to run forever, printing the iteration number.
In the second example, we intentionally omit the condition part of the for loop, leaving it blank. This creates an infinite loop as well. However, we use a break statement inside the loop to exit when a specific condition is met. In this case, when i becomes 5, the break statement is executed, and the loop is terminated.
It's important to be cautious when using infinite for loops because they can lead to program hangs or resource exhaustion if not used correctly. Make sure to include a way to exit the loop, such as a break statement or a condition that eventually becomes false.
Infinite for loops can be useful in scenarios where you want to continuously perform a task until a certain condition is met or until the program is manually stopped. However, they should be used with care and only when necessary.
Frequently Asked Questions
Can an infinite for loop be used in real-world scenarios?
Yes, infinite for loops can be used in situations where you want to continuously perform a task until a specific condition is met or the program is manually stopped. However, they should be used with caution and only when necessary.
Is it possible to exit an infinite for loop without using a break statement?
Yes, you can exit an infinite for loop by using a return statement to exit the method or by throwing an exception. However, using a break statement is the most common and straightforward approach.
Can an infinite for loop cause a program to crash?
Yes, an infinite for loop can cause a program to hang or crash if it consumes too many system resources or if there is no way to exit the loop. It's crucial to ensure that there is a mechanism to terminate the loop when needed.
Conclusion
In this article, we learned about infinite for loop in Java. We looked at the syntax of a for loop and how it can be modified to create an infinite loop. We provided examples to show the use of infinite for loops and explained how they can be used with break statements to exit the loop based on a specific condition. Moreover, we discussed the importance of using infinite for loops cautiously and ensuring that there is a way to terminate the loop when necessary.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.