Java While Loop

Last Updated : 29 Dec 2025

In programming, loops play a pivotal role in iterating over a set of statements repeatedly until a specific condition is met. One such loop in Java is the 'while' loop, known for its simplicity and versatility.

What Is while Loop in Java?

The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.

The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop.

Syntax pf the Java While Loop

It has the following syntax:

Here, condition is a boolean expression that determines whether the loop should continue iterating or not. The statements within the curly braces are executed repeatedly as long as the condition evaluates to true.

Parts of while Loop

The different parts of while loop are as follows:

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update expression. When the condition becomes false, we exit the while loop.

Example:

2. Update Expression: Every time the loop body is executed, this expression increments or decrements loop variable.

Example:

while Loop Example

This example shows the basic usage of a while loop in Java. Here, we want to print numbers from 1 to 5:

Explanation:

In this example, the loop starts with the variable i initialized to 1. The condition i <= 5 is checked before each iteration, and the loop continues as long as the condition is true. Inside the loop, the value of i is printed and then increased by 1, which prevents the loop from running infinitely.

Java While Loop Flowchart

Here, the important thing about while loop is that, sometimes it may not even execute. If the condition to be tested results into false, the loop body is skipped and first statement after the while loop will be executed.

flowchart of java while loop

More Examples on while Loop

Here, we are going to discuss several examples to demonstrate the while loop in Java.

Example 1: Printing Numbers from 1 to 10

In the following example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.

Example

Compile and Run

Output:

1
2
3
4
5
6
7
8
9
10

Example 2: Finding Factorial of a Number

Let us take an example to demonstrate how to find the factorial of a number using while loop in Java.

Example

Compile and Run

Output:

Factorial of 5 is: 120

Java Nested While Loop

A while loop inside the while loop is called nested while loop in Java. The basic syntax of nested while loop is given below:

Syntax of Java Nested While Loop

It has the following syntax:

Example 1: Printing Row and Column Values

This example demonstrates the use of a nested while loop in Java, where the inner loop runs completely for each iteration of the outer loop.

Example

Compile and Run

Output:

1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
3 2
3 3
3 4
3 5

Example 2: Printing Multiplication Table

This program uses a nested while loop to print the multiplication table from 1 to 10 in a structured format.

Example

Compile and Run

Output:

1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100 

Java Infinitive while Loop

While loops can inadvertently become infinite if the condition always evaluates to true. For instance: If we pass true in the while loop, it will be infinitive while loop.

Syntax of Java infinitive while Loop

It has the following syntax:

This loop will continue indefinitely because the condition true is always true. Therefore, it is crucial to ensure that the condition in a while loop eventually becomes false to exit the loop.

Example: Java Inifinitive while Loop

Let us take an example to demonstrate the Java infinitive while Loop.

Output:

infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

Preventing Infinite Loops

To prevent infinite loops, it is essential to incorporate mechanisms that modify the loop's condition within the loop block. For instance, using a variable to control the loop:

In this example, the loop will execute five times, incrementing the count variable with each iteration until it reaches 5, satisfying the condition and ending the loop.

Example: prevent infinite loops with a while loop

The following example demonstrates how to prevent infinite loops with a while loop:

Output:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4

The while loop in Java offers a versatile mechanism for executing repetitive tasks based on a condition. Its simplicity and flexibility make it a valuable tool for various programming scenarios, from simple iteration to complex input validation. However, care must be taken to prevent infinite loops by ensuring that the loop's condition eventually evaluates to false. Understanding and mastering the while loop is essential for any Java programmer aiming to write efficient and reliable code.