Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this quick tutorial, we'll explore ways to create an infinite loop in Java.

Simply put, an infinite loop is an instruction sequence that loops endlessly when a terminating condition isn't met. Creating an infinite loop might be a programming error, but may also be intentional based on the application behavior.

2. Using while

Let's start with the while loop. Here we'll use the boolean literal true to write the while loop condition:

public void infiniteLoopUsingWhile() {
    while (true) {
        // do something
    }
}

3. Using for

Now, let's use the for loop to create an infinite loop:

public void infiniteLoopUsingFor() {
    for (;;) {
        // do something
    }
}

4. Using do-while

An infinite loop can also be created using the less common do-while loop in Java. Here the looping condition is evaluated after the first execution:

public void infiniteLoopUsingDoWhile() {
    do {
        // do something
    } while (true);
}

5. Conclusion

Even though in most of the cases we'll avoid creating infinite loops but there could be some cases where we need to create one. In such scenarios, the loop will terminate when the application exits.

The above code samples are available in the GitHub repository.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!