Java While Loop
A Java while loop repeats a block of code as long as a boolean condition is true. It is useful when you do not know exactly how many times the code should run before the loop starts.
Like if statements, while loops use a condition in parentheses. The difference is that a loop can run the same block many times.
Basic Syntax
A while loop checks its condition before each run. If the condition is true, Java runs the loop body. Then it goes back and checks the condition again.
public class Main {
public static void main(String[] args) {
int count = 1;
while (count <= 3) {
System.out.println("Count: " + count);
count++;
}
}
}
Output:
Count: 1
Count: 2
Count: 3
How It Works
The variable count starts at 1. The condition count <= 3 is true, so Java prints the current value and then runs count++, which adds 1.
After each run, Java checks the condition again. When count becomes 4, the condition is false, so the loop stops. Code after the loop would continue from there.
Updating The Loop Variable
Most while loops need something inside the loop body that moves the program toward stopping. This is often a counter update, but it can also be a changed boolean value or a new value read from input.
public class Main {
public static void main(String[] args) {
int number = 2;
while (number <= 10) {
System.out.println(number);
number += 2;
}
}
}
Output:
2
4
6
8
10
Here, number += 2 increases the value by 2 each time. Without that update, number would stay at 2, and the condition would never become false.
Looping Until A Condition Changes
A while loop is also useful when the stopping point depends on a condition instead of a fixed number of repeats. The next example keeps doubling a value until it is large enough.
public class Main {
public static void main(String[] args) {
int savings = 25;
while (savings < 200) {
savings *= 2;
System.out.println("Savings: " + savings);
}
}
}
Output:
Savings: 50
Savings: 100
Savings: 200
The loop starts because savings is less than 200. Each run doubles the value. After printing 200, Java checks again, sees that savings < 200 is false, and exits the loop.
When The Loop May Not Run
Because a while loop checks the condition first, the body might run zero times. This is correct behavior when the condition is already false.
public class Main {
public static void main(String[] args) {
int lives = 0;
while (lives > 0) {
System.out.println("Keep playing");
lives--;
}
System.out.println("Game over");
}
}
Output:
Game over
The loop body does not run because lives > 0 is false from the start. Java skips the loop and prints the statement after it.
Common Mistakes
- Forgetting to update the value used in the condition, which can create an infinite loop.
- Using a condition that is already false when you expected the loop to run.
- Putting a semicolon right after the
whilecondition, which creates an empty loop body. - Changing the wrong variable inside the loop.
Takeaway: a Java while loop repeats code while its condition stays true, so always make sure the loop has a clear way to stop.
