Bird
0
0

How can you modify nested loops to skip printing when the outer and inner loop variables are equal in PHP?

hard📝 Application Q9 of 15
PHP - Loops
How can you modify nested loops to skip printing when the outer and inner loop variables are equal in PHP?
AChange loop conditions to avoid equal values
BAdd an if statement outside both loops: if ($i == $j) continue;
CUse break instead of continue inside inner loop
DAdd an if statement inside inner loop: if ($i == $j) continue;
Step-by-Step Solution
Solution:
  1. Step 1: Understand where to check condition

    The condition to skip printing when variables are equal must be inside the inner loop.
  2. Step 2: Use continue to skip current iteration

    Using continue inside inner loop skips printing when $i == $j without stopping loops.
  3. Final Answer:

    Add an if statement inside inner loop: if ($i == $j) continue; -> Option D
  4. Quick Check:

    Use continue inside inner loop to skip equal values [OK]
Quick Trick: Use continue inside inner loop to skip unwanted cases [OK]
Common Mistakes:
  • Placing condition outside loops
  • Using break which stops loops entirely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes