Recall & Review
beginner Click to reveal answer
What is a
while loop in PHP?A
while loop in PHP repeats a block of code as long as a given condition is true. It checks the condition before each iteration.beginner Click to reveal answer
When does the
while loop stop executing?The
while loop stops when the condition becomes false. It does not run the loop body if the condition is false at the start.intermediate Click to reveal answer
What happens if the
while loop condition is always true?If the condition is always true, the
while loop runs forever, causing an infinite loop unless stopped manually or by a break statement.beginner Click to reveal answer
Explain the execution order of a
while loop.First, the condition is checked. If true, the loop body runs. Then the condition is checked again. This repeats until the condition is false.
intermediate Click to reveal answer
How can you safely avoid infinite loops with
while?Make sure the loop condition will eventually become false by changing variables inside the loop. Also, use break statements if needed.
What does a
while loop check before running its code block?✗ Incorrect
A
while loop checks the condition before each iteration to decide if it should run the loop body.What happens if the
while loop condition is false at the start?✗ Incorrect
If the condition is false initially, the
while loop does not run at all.Which of these can cause an infinite
while loop?✗ Incorrect
If the condition never becomes false, the loop runs forever, causing an infinite loop.
How do you stop a
while loop manually inside the loop?✗ Incorrect
The
break statement immediately stops the loop execution.What is the main difference between
while and do-while loops?✗ Incorrect
while checks condition first, so may not run; do-while runs loop body once before checking.Describe how a
while loop works in PHP from start to finish.Think about what happens before and after running the code inside the loop.
You got /4 concepts.
Explain how to prevent infinite loops when using a
while loop.Consider what makes the loop stop running.
You got /4 concepts.
