⏱️ 7 min read • Beginner Level • Lesson 26
Completed on . You can revise this lesson or continue to the next topic.
The while loop in Java is used to execute a block of code repeatedly
as long as a given condition remains true. It is an entry-controlled loop,
which means the condition is checked before the loop body executes.
Before learning the while loop, you should understand
Java if statement,
Java operators, and
Java variables.
A while loop is a looping statement that repeats a block of code while
the condition is true. If the condition is false at the beginning, the loop body will
not execute even once.
The condition is checked before every iteration.
The loop body executes only when the condition is true.
A proper update statement is usually required to avoid an infinite loop.
A while loop usually has three important parts: initialization, condition, and update.
initialization;
while (condition) {
// loop body
update;
}
while must be a boolean expression.
For example, i <= 5, count > 0, or isRunning == true.
The following diagram shows how a while loop works. Java checks the condition first. If the condition is true, the loop body executes. After that, the condition is checked again.
while statement.true, the loop body executes.false, the loop stops.
The following program prints Hello five times using a while loop.
public class WhileDemo {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i + ". Hello");
i++;
}
}
}
Output:
1. Hello 2. Hello 3. Hello 4. Hello 5. Hello
i is initialized with value 1.i <= 5 is checked before every iteration.i with Hello.i++ increases the value of i by 1.i becomes 6, the condition becomes false and the loop stops.
This example calculates the sum of numbers from 1 to 5.
public class SumWhileLoop {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while (i <= 5) {
sum = sum + i;
i++;
}
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 15
A while loop can also be used to count backward.
public class ReverseWhileLoop {
public static void main(String[] args) {
int i = 5;
while (i >= 1) {
System.out.println(i);
i--;
}
}
}
Output:
5 4 3 2 1
An infinite loop happens when the condition never becomes false. This usually happens when the update statement is missing or incorrect.
public class InfiniteWhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println(i);
// i++ is missing, so this loop never ends
}
}
}
false. Otherwise, the while loop may run forever.
| while Loop | for Loop |
|---|---|
| Used when the number of iterations is not always known. | Used when the number of iterations is usually known. |
| Initialization, condition, and update are written separately. | Initialization, condition, and update are usually written in one line. |
| Good for condition-based repetition. | Good for counter-based repetition. |
= instead of == in conditions.while loop repeats code while the condition is true.?? Test your understanding with a quick quiz
Topic: While-loop | Language: Java
int i = 5;
while (i >= 1) {
System.out.println(i);
i--;
}int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}int i = 1;
while (i <= 5) {
System.out.println(i);
}?? Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.