Recall & Review
beginner
What is the main purpose of a while loop in Java?
A while loop repeats a block of code as long as a specified condition is true. It helps automate repetitive tasks without writing the same code multiple times.
Click to reveal answer
beginner
How does a while loop differ from an if statement?
An if statement runs code once if a condition is true, while a while loop keeps running the code repeatedly as long as the condition stays true.
Click to reveal answer
intermediate
Why is a while loop useful when the number of repetitions is not known in advance?
Because a while loop checks the condition before each repetition, it can keep running until something changes, like user input or a calculation result, making it flexible for unknown repetition counts.
Click to reveal answer
intermediate
What happens if the condition in a while loop never becomes false?
The loop will run forever, causing an infinite loop. This can freeze or crash the program, so it’s important to ensure the condition will eventually become false.
Click to reveal answer
beginner
Give a real-life example where a while loop is needed.
Imagine filling a bucket with water until it is full. You don’t know how many scoops it will take, so you keep scooping water while the bucket is not full. This is like a while loop checking a condition repeatedly.
Click to reveal answer
What does a while loop do in Java?
✗ Incorrect
A while loop repeats the code block as long as the condition remains true.
When should you use a while loop instead of a for loop?
✗ Incorrect
While loops are best when you don’t know how many times you need to repeat the code.
What is a risk of using a while loop without careful condition checks?
✗ Incorrect
If the condition never becomes false, the loop runs forever, freezing the program.
Which of these is a correct while loop condition?
✗ Incorrect
The condition must be inside parentheses and use a comparison operator.
What happens if the while loop condition is false at the start?
✗ Incorrect
If the condition is false initially, the loop body is skipped.
Explain why a while loop is useful when you don’t know how many times you need to repeat an action.
Think about situations where you wait for something to happen before stopping.
You got /3 concepts.
Describe what can go wrong if a while loop’s condition never becomes false and how to prevent it.
Consider what happens if the loop never stops.
You got /4 concepts.