The while loop is Java’s most fundamental looping statement. It repeats
statement or block while its controlling expression (termination condition) is true.

The while statement continually executes a block of statements while a particular condition is true.
Its syntax can be expressed as:

while (expression) { statement;}

The while statement evaluates expression, which must return a boolean value. If the expression is true, then the while loop executes the statement in the while block. The while loop continues testing the expression and executing its block until the controlling expression evaluates to false.
while loop can be used to print the values from 1 through 5. It is shown in the following WhileDemo program.

Here x is initialized to 1. In the while condition in LINE A, it checks for the condition 1 <= 5, since it is true it goes into the block and prints x = 1. Then it increments the x value by one. With x value as 2 and again it checks for the condition 2 <= 5 and prints the output as x = 2. This repeats for 3 more times, then x will become 6. Since the condition 6 <= 5 becomes false, the control comes out of while loop.