Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

A while loop in Java is an entry-controlled control flow statement that repeats a block of code as long as a Boolean condition stays true. The condition is checked before every iteration, so if it is false the first time, the body never runs. As soon as the condition becomes false, the loop ends and execution continues with the statement after the loop. The short program below uses a while loop to print the numbers 1 to 5.
class WhileDemo {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
}
}Output:
1
2
3
4
5The while loop has a single Boolean condition followed by a body enclosed in braces.
while (condition) {
// statements to run while the condition is true
// update the loop variable here
}The execution follows a fixed cycle every time the loop runs.
A while loop is a natural fit for accumulating a running total. Here it adds the numbers 1 through 10.
class SumDemo {
public static void main(String[] args) {
int i = 1, sum = 0;
while (i <= 10) {
sum += i;
i++;
}
System.out.println("Sum of 1 to 10 = " + sum);
}
}Output:
Sum of 1 to 10 = 55Counting down toward the value that ends the loop works just as well as counting up. This program multiplies down from a number to compute its factorial.
class FactorialDemo {
public static void main(String[] args) {
int n = 5;
long factorial = 1;
while (n > 0) {
factorial *= n;
n--;
}
System.out.println("Factorial of 5 = " + factorial);
}
}Output:
Factorial of 5 = 120The do-while loop is the exit-controlled cousin of the while loop. It runs the body first and checks the condition afterward, so the body always executes at least once even when the condition is false from the start. Note the semicolon after the closing while.
class DoWhileDemo {
public static void main(String[] args) {
int x = 10;
do {
System.out.println("Executed once even though 10 > 5 is false");
} while (x < 5);
}
}Output:
Executed once even though 10 > 5 is falseThe table below summarizes how the two loops differ.
| Aspect | while | do-while |
|---|---|---|
| Condition check | Before the body (entry-controlled) | After the body (exit-controlled) |
| Minimum runs | Zero, if the condition starts false | One, always |
| Best for | Looping that may not run at all | Work that must happen before validation, like menus |
Writing while(true) creates a deliberate infinite loop. To make it terminate, you place a break statement inside it, which immediately exits the nearest enclosing loop when a stop condition is met.
class BreakDemo {
public static void main(String[] args) {
int i = 1;
while (true) {
if (i == 4) {
System.out.println("Stopped at 4");
break;
}
System.out.println(i);
i++;
}
}
}Output:
1
2
3
Stopped at 4The continue statement skips the rest of the current iteration and jumps back to the condition test. The program below prints only the odd numbers from 1 to 10 by continuing past the even ones. Notice that the loop variable is incremented before continue, otherwise the loop would stall.
class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}Output:
1
3
5
7
9A common real-world use of the while loop is reading values until the user enters a stop value. The program below keeps adding numbers until it reads -1, the sentinel.
import java.util.Scanner;
class SentinelDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int total = 0;
int value = sc.nextInt();
while (value != -1) {
total += value;
value = sc.nextInt();
}
System.out.println("Total = " + total);
}
}Input: 5 10 15 -1
Output:
Total = 30while (condition); gives the loop an empty body, so it spins forever without running the statements you intended.Once your loop-driven Java logic is correct, you can run the Selenium or unit tests that depend on it across thousands of real browser and OS combinations on the Selenium Automation, so behaviour stays consistent everywhere your code runs.
The while loop is entry-controlled, so the Boolean condition is evaluated before every iteration, including the very first one. If the condition is false when the loop is reached, the body never runs even once.
A while loop checks the condition before the body, so it can run zero times. A do-while loop checks the condition after the body, so it always runs the body at least once. Use do-while when the work must happen first, such as showing a menu before validating the user's choice.
An infinite while loop usually means the variable in the condition is never updated, is updated in the wrong direction, or a stray semicolon after while(condition) created an empty body. Make sure each iteration moves the loop variable toward the value that makes the condition false.
Use a for loop when you know the number of iterations in advance, since its header keeps the initialization, condition, and update together. Use a while loop when the number of iterations is unknown and depends on a runtime condition, such as reading input until a sentinel value or a stop flag is set.
break immediately exits the nearest enclosing loop, so control jumps to the statement after the loop. continue skips the rest of the current iteration and jumps straight back to the condition test for the next iteration.
Yes. If you omit the braces, only the single statement immediately after while(condition) is treated as the loop body. This is a common source of bugs, so it is safer to always wrap the body in braces.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance