Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop with break
What is the output of this PHP code?
PHP
<?php for ($i = 0; $i < 5; $i++) { if ($i == 3) { break; } echo $i . ' '; } ?>
Attempts:
2 left
💡 Hint
The loop stops when the condition inside the if is true.
✗ Incorrect
The loop starts at 0 and prints each number. When i equals 3, the break stops the loop before printing 3.
❓ Predict Output
intermediate2:00remaining
For loop with continue statement
What will this PHP code output?
PHP
<?php for ($i = 0; $i < 5; $i++) { if ($i % 2 == 0) { continue; } echo $i . ' '; } ?>
Attempts:
2 left
💡 Hint
The continue skips the current loop iteration when the number is even.
✗ Incorrect
The loop prints only odd numbers because it skips even numbers using continue.
❓ Predict Output
advanced2:00remaining
For loop with multiple expressions in control
What is the output of this PHP code?
PHP
<?php for ($i = 0, $j = 5; $i < 3; $i++, $j--) { echo $i . '-' . $j . ' '; } ?>
Attempts:
2 left
💡 Hint
Both variables change each loop iteration.
✗ Incorrect
The loop runs 3 times. Each time, i increases and j decreases, printing pairs.
❓ Predict Output
advanced2:00remaining
For loop with missing increment expression
What will this PHP code output?
PHP
<?php for ($i = 0; $i < 3;) { echo $i . ' '; $i++; } ?>
Attempts:
2 left
💡 Hint
Increment can be inside the loop body.
✗ Incorrect
The loop increments i inside the body, so it prints 0,1,2 and stops.
🧠 Conceptual
expert2:00remaining
Understanding for loop execution order
In PHP, what is the correct order of execution for the parts of a for loop?
Attempts:
2 left
💡 Hint
Initialization happens once, then condition is checked before each loop.
✗ Incorrect
First, initialization runs once. Then condition is checked before each iteration. If true, loop body runs, then increment runs, then condition checked again.