In Java, the code is executed sequentially in a specified order from top to bottom. There are statements in Java that can be used in controlling the code flow. These statements have three different varieties and one of these are the Loop statements. The loop statements in Java run a set of instructions until the condition is “not true”. Java supports several types of loops including the “for”, “do-while”, and the “while” loop.
In this article, we will specifically discuss the working of a while loop in Java.
How While Loop Work in Java?
The while loop in Java is executed in the block of code where the user has no idea related to the count of iterations. It iterates over the number of statements in the code multiple times. Since it checks for the condition at the start of the loop, therefore, it is termed an “Entry controlled loop”. When the condition appears to be true, the loop is executed else the loop doesn’t execute.
The syntax of the while loop is elaborated as:
while(condition){
//The looping statements
}
Example 1: Implementation of While Loop to Print the Strings
The example code below implements the While loop in Java.
//Declare a Class for While Loop
class Example1{
public static void main(String args[])
{
System.out.println("The While Loop in Java");
//Initialize the array
int x = 1;
//While loop to test
while (x < 8) {
System.out.println("Java Programming");
//update the expression
x++;
}
}
}
In the above Java code:
- A class is declared as “Example1” for the implementation of a while loop.
- The integer is first initialized as x=1.
- The next step involves the while loop which will print the output seven times.
- The output of the while loop is printed using the println() method and the expression is also updated.
Output
The output below shows that “Java Programming” is printed seven times using a while loop.

Example 2: Calculate the Sum of Positive Numbers
The code below calculates the sum of the specified numbers using the While loop in Java.
//Declare a class in Java to add specific numbers.
class SumExample{
public static void main(String args[])
{
System.out.println("CALCULATE THE SUM OF THE NUMBERS");
//Initializing the integers and the result
int a = 1, result = 0;
//Exit when a is greater than 15
while (a <= 15) {
//Add the numbers from 1 to 15
result = result + a;
a++;
}
//Print the result
System.out.println("The Sum of the Numbers are : " + result);
}
}
In the above code block:
- A class is declared as “SumExample” to add the numbers using a while loop.
- In the main() method of Java, the strings a and result are initialized as 1 and 0 respectively.
- The next step involves a while loop that adds the number from 1 to 15.
- The sum is printed as the “result” using the println() method.
Output
The output below describes the result of numbers from 1 to 15 is 120.

Conclusion
The while loop in Java checks the condition at the start of the loop and executes the loop body for the condition that is true otherwise it won’t execute the code specified within the loop’s body. This article has effectively elaborated the working of the while loop in Java through code examples.