
Java While Loop in Java
While Loop in Java
In Java, the while
loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop continues as long as the specified condition evaluates to true
.
Syntax of While Loop
while (condition) { // Code block to be executed}
condition: A boolean expression that is evaluated before each iteration. If it's
true
, the code inside the loop is executed.The loop continues to execute as long as the condition remains
true
. Once the condition becomesfalse
, the loop terminates.
Flow of a While Loop
The condition is checked.
If the condition is
true
, the code inside the loop executes.After the code is executed, the condition is checked again.
This continues until the condition becomes
false
, at which point the loop terminates.
Example: Basic While Loop
The following example demonstrates a simple while loop that prints numbers from 1 to 5.
public class WhileLoopExample { public static void main(String[] args) { int i = 1; // Initialize the variable // While loop that runs while i is less than or equal to 5 while (i <= 5) { System.out.println(i); i++; // Increment the value of i } }}
Explanation:
The loop starts with
i = 1
.It checks if
i <= 5
. Since the condition is true, the numberi
is printed.The variable
i
is then incremented (i++
), and the condition is checked again.This continues until
i
becomes 6, at which point the conditioni <= 5
becomes false, and the loop terminates.
Output:
12345
Infinite While Loop
If the condition in a while
loop always evaluates to true
, the loop will run infinitely, creating an infinite loop. This should generally be avoided unless you have a specific reason (such as waiting for user input or some event to happen).
// Infinite loop (be cautious with this!)while (true) { System.out.println("This will run forever!");}
Important: You can break out of an infinite loop using the break
statement, or by terminating the program manually.
While Loop with User Input
Here’s an example of using a while
loop with user input:
import java.util.Scanner;public class WhileLoopUserInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String userInput; // The loop runs until the user enters "exit" while (true) { System.out.print("Enter 'exit' to quit: "); userInput = scanner.nextLine(); if (userInput.equalsIgnoreCase("exit")) { break; // Exit the loop if the user enters "exit" } System.out.println("You entered: " + userInput); } scanner.close(); }}
Explanation:
The
while
loop continues until the user inputs the wordexit
.The
break
statement inside the loop terminates the loop when the condition is met.
Common Use Cases for While Loop
Reading Data Until a Condition is Met
Example: Continuously asking for user input until a valid response is given.
Counting or Iterating with a Condition
Example: Counting down from a number until it reaches zero.
Infinite Loop (with a Break Condition)
Example: A menu system where the user can choose different actions until they choose to exit.
Do-While Loop
A related loop in Java is the do-while
loop. Unlike the while
loop, the do-while
loop guarantees that the code will execute at least once, even if the condition is false
from the start.
Syntax of a Do-While Loop:
do { // Code block to be executed} while (condition);
Let me know if you'd like to see more examples or if you have questions about how to use the while
loop in specific scenarios!