A while loop helps repeat a task as long as a condition is true. It saves time and avoids writing the same code again and again.
Why while loop is needed in Java
while (condition) { // code to repeat }
The condition is checked before each repetition.
If the condition is false at the start, the code inside the loop may never run.
count is less than 5.int count = 0; while (count < 5) { System.out.println(count); count++; }
running becomes false inside the loop.boolean running = true; while (running) { // do something running = false; // stop after one run }
This program keeps asking the user to enter a positive number. It repeats the question until the user enters a number greater than zero.
import java.util.Scanner; public class WhileLoopExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = 0; while (number <= 0) { System.out.print("Enter a positive number: "); number = scanner.nextInt(); if (number <= 0) { System.out.println("That's not positive. Try again."); } } System.out.println("You entered: " + number); scanner.close(); } }
Be careful to change something inside the loop that eventually makes the condition false, or the loop will run forever.
Use while when you don't know how many times the loop should run before starting.
While loops repeat code as long as a condition is true.
They are useful when the number of repetitions is not known in advance.
Always make sure the loop condition will become false to stop the loop.
Practice
while loop in Java?Solution
Step 1: Understand the purpose of
Thewhileloopwhileloop repeats code as long as a condition is true, useful when we don't know how many times to repeat.Step 2: Compare with other options
Options A, B, and C do not describe repeating code based on a condition.Final Answer:
To repeat code when the number of repetitions is unknown -> Option AQuick Check:
While loop = repeat unknown times [OK]
- Thinking while loops run a fixed number of times
- Confusing while with if statements
- Using while loop without changing the condition
while loop in Java?Solution
Step 1: Recall Java syntax for while loops
Java requires parentheses around the condition and curly braces for the loop body:while (condition) { }.Step 2: Check each option
while (condition) { } matches the correct syntax; others use wrong brackets or missing parentheses.Final Answer:
while (condition) { } -> Option DQuick Check:
Correct syntax = while (condition) { } [OK]
- Omitting parentheses around the condition
- Using square brackets instead of parentheses
- Placing condition outside parentheses
int count = 1;
while (count <= 3) {
System.out.print(count + " ");
count++;
}Solution
Step 1: Analyze the loop condition and variable
The loop starts with count = 1 and runs while count is less than or equal to 3.Step 2: Trace each iteration output
It prints 1, then increments to 2, prints 2, increments to 3, prints 3, increments to 4, then stops because 4 > 3.Final Answer:
1 2 3 -> Option AQuick Check:
Prints 1 to 3 with spaces [OK]
- Expecting 4 to be printed
- Confusing <= with <
- Forgetting to increment count
int i = 0;
while (i < 5) {
System.out.println(i);
}Solution
Step 1: Check loop condition and variable update
The loop condition isi < 5, butiis never changed inside the loop.Step 2: Understand effect of no update
Sinceistays 0, the condition is always true, causing an infinite loop.Final Answer:
Infinite loop because i is never updated -> Option CQuick Check:
No increment = infinite loop [OK]
- Forgetting to increment or change loop variable
- Assuming loop stops automatically
- Confusing syntax errors with logic errors
while loop for this?Solution
Step 1: Understand the goal
We want to keep reading input until the user types "exit".Step 2: Analyze each option
String input = ""; while (!input.equals("exit")) { input = scanner.nextLine(); System.out.println("You typed: " + input); } initializes input as empty string, then loops while input is not "exit", reading new input inside the loop. This matches the requirement.
String input = scanner.nextLine(); while (input.equals("exit")) { System.out.println("You typed: " + input); input = scanner.nextLine(); } loops only while input equals "exit", which is opposite.
while (true) { String input = scanner.nextLine(); if (input.equals("exit")); break; System.out.println("You typed: " + input); } uses an infinite loop with break, but the semicolon after if makes break always execute, stopping after first input.
String input; do { input = scanner.nextLine(); System.out.println("You typed: " + input); } while (!input.equals("exit")); uses ado-whileloop, not awhileloop.Final Answer:
String input = ""; while (!input.equals("exit")) { input = scanner.nextLine(); System.out.println("You typed: " + input); } -> Option BQuick Check:
While loop with condition != "exit" works [OK]
- Using wrong loop condition
- Confusing while with do-while
- Not updating input inside loop
Ready to prove it?
Pass the test and this pattern moves unknown
