Infinite Loop in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In Java, an infinite loop endlessly executes without a stopping condition, potentially causing resource overuse or application crashes. While it can result from programming errors, it might serve intentional purposes based on specific application needs. Recognizing Java's loop types and behaviors is vital for effectively handling and preventing unintended infinite loops.

Using While Loop

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of While Loop

The code part inside the body of while loop is executed if the condition is true else the while loop is terminated. So we have to provide a condition that will always evaluate to true and this can be achieved by passing true itself as the condition in the above loop.

Code:

Output:

Explanation:

If we try to run the above code, the code will print Hello World as the while loop keeps on executing for infinite time.

IMPORTANT: If there is a time limit on the period of execution, TimeLimitExceeded error will be thrown.

Using For Loop

Syntax of for loop

Let's have a brief recap of for loop in Java, the initialization expression is used to initialize the counter, and the condition expression tests whether we have to execute the body of the for loop or we have to terminate the loop. The updation expression is executed after each execution of the for loop.

Let's have an example of for loop in Java,

Output:

Explanation:

  • In the initialization part, the variable i is assigned the value 00, and if i is less than 33 the body of the for loop is executed.
  • After each execution of the for loop, i is incremented by 11. After executing the for loop for i=2i=2, i becomes 33, and the condition i<3i<3 evaluates to false thus resulting in the termination of the for loop.

So in the case of for loop in Java, we can also pass the condition expression as true so that the for loop executes for infinite time.

Output:

Explanation:

  • Here, the body of the loop is executed till i is less than 3.
  • However, instead of incrementing the value of i, we are decreasing it in the updation statement.
  • As a result, the condition is always true and the loop executes infinitely.

But here is a twist, in the case of for loop the expressions initialization, condition, and updation are optional whereas in the case of while loop the condition expression was mandatory. So if we leave this expression as empty, the above for loop will execute for infinite time.

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using a do-While Loop

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Syntax of do-while loop

The do-while loop is similar to the while loop in Java, but with one minor change. The do-while loop executes the do block of the loop and then checks the condition. So the do-while loop is executed atleast once even if the condition is false.

Let's have an example of this:

Output:

And just like the while loop, the condition expression is mandatory.

Like we did in the while loop, we will pass true as the condition and the above loop will execute for infinite time.

Output:

Now we have learned the infinite loops in Java, let's move on to the intentional infinite loops in Java.

Intentional Infinite Loops

Sometimes we intentionally want an infinite loop for example in a web server. A typical web server accepts a request for a web page, such as a communicative message, and returns a web page while waiting for the next request.

Thus the web server needs to be active always to read and process upcoming requests. This can be achieved by using infinite loops as,

or,

Conclusion

  • An infinite loop is a sequence of instructions that loops indefinitely unless the program is terminated or the system crashes.
  • In the case of a while loop in java we can pass true as the condition to execute the while loop for infinite time, in the case of a for loop in java we can leave the expression empty to execute the for loop for infinite time, in the case of a do-while loop in java we can pass true as the condition to execute the do-while loop for infinite time.
  • Most of the time infinite loops in Java are considered a programming error, but these can be intentional such as in the case of a web server.
  • If there is a limit on the period of execution of a program, TimeLimitExceeded (TLE) error is thrown.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more