// While loop example
public class WhileLoopExample {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}
While Loop Java: A Beginner's Guide
The while loop Java is a control flow statement that allows you to execute a block of code repeatedly as long as a certain condition is met. It's commonly used when the number of iterations is unknown.
In this tutorial, we'll explore the syntax, usage, and examples of while loops in Java, as well as related concepts like do-while loops, infinite loops, and more.
While Loop Syntax
The basic syntax of a while loop Java is as follows:
while (condition) {
// code to be executed
}
The condition is a boolean expression that's evaluated at the beginning of each iteration. If it's true, the code inside the loop is executed.
Do-While Loop
A do-while loop is similar to a while loop, but the condition is evaluated at the end of each iteration. This means the code inside the loop is always executed at least once.
// Do-while loop example
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
}
}
Infinite Loops
An infinite loop is a loop that continues indefinitely because the condition is always true. This can happen intentionally or unintentionally.
// Infinite loop example
public class InfiniteLoopExample {
public static void main(String[] args) {
while (true) {
System.out.println("Infinite loop!");
}
}
}

To avoid infinite loops, make sure the condition is eventually false or use a break statement to exit the loop.
Break and Continue Statements
The break statement exits the loop immediately, while the continue statement skips the current iteration and moves on to the next one.
// Break and continue example
public class BreakContinueExample {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
if (i == 3) {
break;
}
System.out.println(i);
i++;
}
}
}
Nested While Loops
Nested while loops are used when you need to iterate over multiple conditions. The inner loop is executed for each iteration of the outer loop.
// Nested while loop example
public class NestedWhileLoopExample {
public static void main(String[] args) {
int i = 0;
while (i < 3) {
int j = 0;
while (j < 2) {
System.out.println("i: " + i + ", j: " + j);
j++;
}
i++;
}
}
}
While Loop vs For Loop
Both while loops and for loops are used for iteration, but they have different use cases. While loops are more flexible and suitable for complex conditions, for loops are more concise and easier to read.
// For loop example
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
Common Patterns
Input Validation
While loops are often used for input validation, where the user is prompted to enter a value until it meets certain criteria.
// Input validation example
public class InputValidationExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter a number between 1 and 10: ");
int input = scanner.nextInt();
if (input >= 1 && input = 10) {
break;
}
}
}
}

Game Loops
While loops are also used in game development to create game loops, where the game state is updated and rendered repeatedly.
// Game loop example
public class GameLoopExample {
public static void main(String[] args) {
while (true) {
// Update game state
// Render game
// Handle user input
}
}
}
While loops are a fundamental concept in Java programming, and understanding how to use them effectively is important for any aspiring developer.
With this tutorial, you've learned the basics of while loops in Java, including syntax, usage, and common patterns. Practice makes perfect, so be sure to try out these examples and experiment with different scenarios.
Want to convert your Java code to another language? Try our CodeConverter tool for fast and accurate conversions!