- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java While Loop
In Java, the while loop is a fundamental control flow statement used to repeatedly execute a block of code as long as a specified boolean condition remains true. It is particularly useful when the number of iterations isn't known beforehand—such as reading lines from a file or waiting for a specific user action.
while loop as a repeating if statement. It checks the condition, runs the code, and then jumps back to the top to check the condition again.
Syntax:
while (condition) {
// Code to execute as long as condition is true
}
Condition:
- The condition must evaluate to a
boolean(true or false). - It is checked before each iteration. If the condition is false the very first time, the code inside the loop will never execute.
- Once the condition becomes false, the program skips the loop and continues with the next line of code following the closing brace.
While Loop Example:
In this example, we use a counter variable to track how many times the loop has run. This is a common pattern for simple repetitions.
int count = 0;
while (count < 5) {
System.out.println("Count is: " + count);
count++; // Increment the counter to eventually break the loop
}
count++). If you omit this, count stays at 0, 0 < 5 remains true forever, and you create an unintended infinite loop.
Infinite While Loop:
An infinite loop occurs when the condition always evaluates to true. While usually a bug, intentional infinite loops are common in server-side programming (listening for requests) or game engines (the "game loop").
while (true) {
System.out.println("This will print forever until the program is stopped.");
// Usually, you would have a 'break' statement inside to exit based on a specific trigger
}
While Loop with Input:
One of the most practical uses for a while loop is handling user interaction. Since you don't know how many times a user will enter a wrong command, a while loop is better suited than a for loop.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equalsIgnoreCase("quit")) {
System.out.print("Enter a command (type 'quit' to exit): ");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("quit")) {
System.out.println("Executing command: " + input);
}
}
System.out.println("Program terminated.");
In the example above, the loop continues to prompt the user until they type the word "quit". This is a standard pattern for command-line interfaces (CLI).
Summary
The while loop is an essential tool in your Java toolkit. It provides the flexibility to handle logic where the exit point depends on dynamic data rather than a fixed count. By mastering the while loop, you can handle user input, process data streams, and manage complex application states effectively. Just remember to always provide a clear path for the loop to terminate!