Recall & Review
beginner
What is the basic structure of a while loop in Java?
A while loop starts with the keyword
while, followed by a condition in parentheses. The loop body is enclosed in braces and runs repeatedly as long as the condition is true.Click to reveal answer
beginner
What happens if the while loop condition is false at the start?
The loop body does not execute even once. The program skips the loop and continues with the next statements after the loop.
Click to reveal answer
beginner
How does the while loop decide whether to run the loop body again?
After each execution of the loop body, the condition is checked again. If it is true, the loop runs again; if false, the loop stops.
Click to reveal answer
intermediate
What can cause an infinite while loop?
If the condition never becomes false, the loop keeps running forever. This usually happens if the loop variable is not updated inside the loop.
Click to reveal answer
beginner
Explain the flow of a while loop with an example: <br>
int i = 0; while (i < 3) { System.out.println(i); i++; }1. Start with i = 0.<br>2. Check if i < 3 (true).<br>3. Print i (0).<br>4. Increase i by 1 (i = 1).<br>5. Repeat steps 2-4 until i is 3.<br>6. When i = 3, condition is false, loop stops.
Click to reveal answer
What happens if the condition in a while loop is false at the start?
✗ Incorrect
If the condition is false initially, the while loop skips the body and continues after the loop.
When is the condition in a while loop checked?
✗ Incorrect
The while loop checks the condition before running the loop body each time.
What can cause an infinite while loop?
✗ Incorrect
If the condition never changes to false, the loop runs forever.
Which keyword is used to start a while loop in Java?
✗ Incorrect
The keyword
while starts a while loop.What happens after the loop body executes in a while loop?
✗ Incorrect
After each loop body execution, the condition is checked to decide if the loop runs again.
Describe the step-by-step execution flow of a while loop in Java.
Think about what happens before and after the loop body runs.
You got /4 concepts.
Explain why a while loop might run forever and how to prevent it.
Consider what controls the loop's stop condition.
You got /4 concepts.