Java While Loop

Java While Loop is used to execute a code block repeatedly in a loop based on a condition.

Use while keyword to write while loop statement in Java.

In this tutorial, we will learn the syntax of while loop statement and demonstrate some of the scenarios of while loop using example programs.

Syntax of While Loop

Following is the syntax of while loop.

</>
Copy
 while(condition) {
     statement(s)
 }

while is the keyword and condition is the expression which evaluates to a boolean value.

When execution comes to while statement, it checks if the condition is true. If it is true, the statement(s) execute and the condition is checked again. It it is true again, the statement(s) execute again. During any iteration, if the condition evaluates to false, the while loop statement is marked complete and the execution goes to subsequent statements after while statement, if any.

Unlike For loop, the developer has to take care of the initialization and update of the control variables. The control variables are those that are involved in the while condition.

Example 1 – Java While Loop

In this example java program, we have a while loop. And this while loop prints numbers from 1 to 5.

Example.java

</>
Copy
public class Example  {

	public static void main(String[] args) {
		
		//initialization
		int i=1;
		while(i<6) {
			System.out.println(i);
			//update
			i++;
		}
	}

}

Run the program, and you will see the following output print to the console.

Output

1
2
3
4
5

Example 2 – Java While Loop – Indefinite

In this example java program, we have a while loop. And this while loop prints numbers from 1 to and so on. The while loop is going to run forever. Because we gave true for condition in the while loop. As the condition never evaluates to false, the while loop runs indefinitely.

Example.java

</>
Copy
public class Example {

	public static void main(String[] args) {
		
		//initialization
		int i=1;
		while(true) {
			System.out.println(i);
			//update
			i++;
		}
	}

}

Run the program, and you will see the following output print to the console. This is only a part of the output. You console would probably scrolling and printing natural numbers like crazy.

Output

1
2
3
4
5

Example 3 – Java While Loop – Break

We can break a while loop using break statement. This breaks the loop even before the while condition evaluates to false.

In this example java program, we have a while loop. And this while loop breaks when i is 4.

Example.java

</>
Copy
public class Example {

	public static void main(String[] args) {
		
		//initialization
		int i=1;
		while(i<6) {
			if(i==4) {
				break;
			}
			System.out.println(i);
			//update
			i++;
		}
	}

}

While loop in the above program breaks when i becomes 4.

1
2
3

Example 4 – Java While Loop – Continue

You can skip the execution of code block by using continue statement.

Note: Make sure that you update control variables. In the following example, it is i. If you don’t update i before executing continue statement, the while loop may remain in infinite loop.

Example.java

</>
Copy
public class Example {

	public static void main(String[] args) {
		
		//initialization
		int i=1;
		while(i<6) {
			if(i==4) {
				i++;
				continue;
			}
			System.out.println(i);
			//update
			i++;
		}
	}

}

Run the program, and you will see the following output print to the console. At continue statement, Java skips the execution of code block any further and continues with the next iteration.

Output

1
2
3
5

Example 5 – Java While Loop – Finding Factorial

In the following java program, we shall find the factorial of a number.

Example.java

</>
Copy
public class JavaTutorial {

	public static void main(String[] args) {
		int n=6;
		
		//initialization
		int factorial = 1;
		int i = 1;
		
		while(i<=n) {
			factorial = factorial*i;
			i++;
		}
		
		System.out.println("Factorial of "+n+" is : "+factorial);
	}

}

Run the program, and you will see the following output print to the console.

Output

Factorial of 6 is : 720

Example 6 – Java While Loop – Array

In this example java program, we shall use while loop to traverse through an array.

Example.java

</>
Copy
public class JavaTutorial {

	public static void main(String[] args) {
		String[] phones = {"Apple", "Android", "Xiaomi", "Lenovo"};
		
		//initialization
		int i = 0;
		
		while(i<phones.length) {
			System.out.println("I have "+phones[i] + " smartphone.");
			i++;
		}
	}

}

While loop iterates array length number of times and you can execute the code block for each of the element in the array.

Output

I have Apple smartphone.
I have Android smartphone.
I have Xiaomi smartphone.
I have Lenovo smartphone.

Conclusion

In this Java Tutorial, we learned how to write while loop in Java and different scenarios/situations to while loop in a Java application.