Java Notes
Complete guide to Java while loop — syntax, execution flow, sentinel-controlled loops, input validation, infinite loops, and comparison with for loop.
The while loop repeats a block of code as long as a condition remains true. It's ideal when you don't know in advance how many iterations you need.
Basic Syntax and Execution
1 2 3 4 5 Loop ended. count = 6 Sum of digits of 12345 = 15
When the Condition is False Initially
While loop was skipped. x = 100
Practical Examples
Reverse a Number
public class ReverseNumber {
public static void main(String[] args) {
int original = 12345;
int number = original;
int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
System.out.println("Original: " + original);
System.out.println("Reversed: " + reversed);
}
}Original: 12345 Reversed: 54321
Input Validation Loop
Enter valid age (0-150): -5 Invalid! Try again. Enter valid age (0-150): 200 Invalid! Try again. Enter valid age (0-150): 25 Accepted age: 25
Fibonacci Sequence
First 10 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34
GCD (Euclidean Algorithm)
public class GCDCalculator {
public static void main(String[] args) {
int a = 48, b = 18;
int origA = a, origB = b;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println("GCD(" + origA + ", " + origB + ") = " + a);
}
}GCD(48, 18) = 6
Infinite While Loop
Attempt #1 Attempt #2 Attempt #3 Max attempts reached. Exiting.
Common Mistakes
- Forgetting to update the condition variable — leads to infinite loop. Always ensure something changes to eventually make the condition false.
- Off-by-one with while — using
<vs<=changes iteration count by 1. Trace through with boundary values. - Condition always true —
while (x > 0)when x is never modified inside the loop = infinite loop. - Modifying condition variable at wrong position — updating at the start vs end of the loop body produces different results.
Interview Questions
Q1: What is the difference between while and for loop?
Answer: Both achieve the same thing, but for is best when the number of iterations is known (counter-based), while while is best when the termination depends on a condition that's not count-based (input validation, searching, processing until a sentinel value).
Q2: Can a while loop execute zero times?
Answer: Yes! If the condition is false from the start, the body never executes. This distinguishes while from do-while (which always executes at least once).
Q3: How do you prevent an infinite while loop?
Answer: Ensure the loop body modifies something that will eventually make the condition false. Common approaches: increment/decrement a counter, read new input, change a boolean flag, or use break with an explicit exit condition.
Q4: Write a while loop to find the number of digits in an integer.
Answer: int digits = 0; int n = number; while (n != 0) { digits++; n /= 10; } — divides by 10 repeatedly until 0, counting iterations. Handle special case: if number is 0, it has 1 digit.
Summary
The while loop is ideal for condition-controlled iteration where the number of repetitions isn't known in advance. The condition is checked before each iteration, so the body may execute zero times. Always ensure progress toward termination to avoid infinite loops. Use while for input validation, sentinel-controlled processing, and algorithms that converge.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for While Loop in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, fundamentals, control, statements
Related Java Master Course Topics