Java — While Loop
Basic while
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Do-while
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Infinite loop
while (true) {
// Break to exit
if (condition) break;
}
Mini Practice
- Write while loop
- Use do-while
- Create counter
- Practice infinite loops
Up Next
Continue with For Loop - For loops.
Related Topics
Frequently Asked Questions about While Loop
What is While Loop in Java?
While Loop is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn While Loop?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn While Loop.
Why is While Loop important in Java?
While Loop is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.