While Loops in Java: Example & Syntax

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Do-While Loops in Java: Syntax & Example

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

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 While Loop
  • 1:16 While Loop Example
  • 2:04 Indefinite While Loop
  • 2:40 Off by One
  • 3:15 Infinite Loop
  • 4:04 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

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 and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

This lesson will cover the Java while statement, which loops through lines of code while a specific value or condition is true. While loops are very powerful, but they should be carefully developed and tested.

While Loop

A while loop is like a loop on a roller coaster, except that it won't stop going around until the operator flips a switch.

A while statement performs an action until a certain criteria is false. This condition uses a boolean, meaning it has a yes/no, true/false, or 0/1 value. The code will keep processing as long as that value is true. Keeping with the example of the roller coaster operator, once she flips the switch, the condition (on/off) is set to Off/False.

For example, if you want to continue executing code until the user hits a specific key or a specified threshold is reached, you would use a while loop.

The basic syntax for a while loop is:

while(condition) {
  //loop code here

If the condition is never met, then the code isn't run at all; the program skips by it.

But what if the condition is met halfway through a long list of code within the while statement? The computer will continue to process the body of the loop until it reaches the last line. Then, it goes back to see if the condition is still true.

You can have multiple conditions in a while statement. For example, you can have the loop run while one value is positive and another negative, like you can see playing out here:

while(j > 2 && i < 0){

The && specifies 'and;' use || to specify 'or.'

While Loop Example

The following code example loops through numbers up to 1,000 and returns all even values:

public static void main(String[] args) {
  int myNumber = 1;
  while(myNumber != 1000) {
   if((myNumber % 2) != 0) {
    myNumber++; }
    else {
    System.out.println(myNumber + " is even");

The code creates an integer and sets the value to 1. The while command then begins processing; it will keep going as long as the number is not 1,000. Note that the statement could also have been written in this much shorter version of the code:

while((myNumber < 1000) {

There's a test within the while loop that checks to see if a number is even (evenly divisible by 2); it then prints out that number. This type of loop could have been done with a for statement, since we know that we're stopping at 1,000.

A while loop is a great solution when you don't know when the roller coaster operator will flip the switch. This type of while loop is called an indefinite loop, because it's a loop where you don't know when the condition will be true.

Let's look at another example that looks at an indefinite loop:

Indefinite While Loop

In keeping with the roller coaster example, let's look at a measure of panic. If we start with a panic rate of 2% per minute, how long will it take to reach 100%? Our while loop will run as long as the total panic rate is less than 100%, which you can see in the code here:

public static void main(String[] args) {
  final double panic_rate = .02;
  int minute;
  minute = 0;
  double total_panic = 0;
  while(total_panic <= 1) {
   total_panic = panic_rate * minute;
  System.out.println("Panic hits 100% after " + minute + " minutes.");

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