While Loops in Java: Example & Syntax

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adunct 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.

While Concept

A while statement performs an action until a certain criteria, or condition is false. This condition uses a boolean, meaning it has a yes/no; true/false; 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:

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

The && specifies AND; use || to specify OR.

Example: While Loop

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

public static void main(String[] args) {
  int myNumber = 1;
  while(myNumber != 1000) {
   if((myNumber % 2) == 0) {
    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:

while((myNumber < 1000) {

There is 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 are 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 you don't know when the condition will be true.

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

Example: 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%:

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.");

The code sets a static rate of panic at .02 (2%), and total panic to 0. The while loop runs as long as the total panic is less than 1 (100%). In the body of the while loop, the panic is increased by multiplying the rate * the minute and adding to the total.

The output is:

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

Register for a free trial

Are you a student or a teacher?
I am a teacher

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back

Earning College Credit

Did you know… We have over 95 college courses that prepare you to earn credit by exam that is accepted by over 2,000 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? 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 free for 5 days!
Create An Account