0
0
PHPprogramming~5 mins

While loop execution model in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
Click to reveal answer
beginner
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.
Click to reveal answer
intermediate
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.
Click to reveal answer
beginner
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.
Click to reveal answer
intermediate
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.
Click to reveal answer
What does a while loop check before running its code block?
AThe loop body
BThe loop condition
CThe loop counter after running
DNothing, it runs once automatically
What happens if the while loop condition is false at the start?
AThe loop never runs
BThe loop runs once
CThe loop runs twice
DThe loop runs infinitely
Which of these can cause an infinite while loop?
ACondition changes to false inside loop
BCondition always false
CCondition always true
DUsing break inside loop
How do you stop a while loop manually inside the loop?
AUsing <code>return</code>
BUsing <code>continue</code>
CUsing <code>exit</code>
DUsing <code>break</code>
What is the main difference between while and do-while loops?
A<code>while</code> checks condition before loop, <code>do-while</code> after
B<code>while</code> runs once, <code>do-while</code> never runs
C<code>while</code> loops forever, <code>do-while</code> does not
DNo difference
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.