Bird
0
0

How can you modify nested loops to skip printing when inner loop variable equals outer loop variable?

hard📝 Application Q9 of 15
Python - For Loop
How can you modify nested loops to skip printing when inner loop variable equals outer loop variable?
Example code:
for i in range(3):
    for j in range(3):
        print(i, j)
AAdd if statement: if i != j: print(i, j)
BChange inner loop range to exclude i
CUse break when i == j
DSwap i and j in print statement
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition to skip printing

    We want to print only when i and j are not equal.
  2. Step 2: Use if statement inside inner loop

    Adding if i != j: before print skips printing when i equals j.
  3. Final Answer:

    Add if statement: if i != j: print(i, j) -> Option A
  4. Quick Check:

    Use condition inside inner loop to control printing [OK]
Quick Trick: Use if condition inside inner loop to skip unwanted prints [OK]
Common Mistakes:
MISTAKES
  • Using break which stops inner loop entirely
  • Changing loop ranges incorrectly
  • Swapping variables without effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes