Copyright

Infinite While Loops in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: What is an Array in Java?

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:00 Infinite While Loops in Java
  • 0:59 Examples
  • 3:07 Using Infinite Loops…
  • 5:06 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed Audio mode

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems, has a PhD in Information Technology Management, and a degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

Infinity is a daunting concept, and in Java, an infinite loop can be a nightmare. In this lesson, we'll break down the concept of an infinite/unending loop in Java and provide examples and caveats.

Infinite While Loops in Java

An infinite while loop in Java is a set of code that would repeat itself forever, unless the system crashes. At a certain point, the data becomes an overload and the program will overflow. This means it will fail.

How do you get in such a predicament? It's actually very easy, and something you should always test for in while loops. Basically, the infinite loop happens when the condition in the while loop always evaluates to true.

This can happen when the variables within the loop aren't updated correctly, or aren't updated at all. Let's say you have a variable that's set to 10 and you want to loop while the value is less than 100. If you don't update the variable within the body of the loop, it will be infinite. This is because 10 is always less than 100. The condition evaluates to true, and the loop begins an infinite run. Java then overflows, and it all comes crashing down.

Examples

Here's an example of a while loop that will run forever. The counter variable is initialized to zero, and the while loop will execute as long as the counter is less than 10. However, the counter is reset to 1 in the while loop. This means that the counter will always be 1, and the loop will run until the system goes into overflow, which could mean a crash.

Although it's fairly obvious that this won't work, because we set the Boolean values to true and false to clearly show that the condition will evaluate to true. It is helpful to see this type of code in action, because it highlights what is really happening in an infinite while loop.

boolean valid = true;
while(valid) {
  System.out.println("Hi");
}

Let's look at another example that is similar. The updater variable never gets changed so it will continue to run until overflow or system crash.

int updater = 1;
while(updater <= 10) {
  System.println(updater);
}
System.out.println("Quit: updater = " + updater);
//the code never runs

When you first look at this code, it does look like it's a valid while statement. And it is, it will compile, but a careful inspection will reveal that it is an infinite while loop. The condition will always evaluate to true.

Most infinite while loops are bad. But that isn't always the case. There are times when you can use an infinite loop to your advantage. The loop might technically be considered infinite, but if you provide a way out of the code, it is not as nasty as true runaway code.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support