Bird
0
0

Find the error in this nested while loop code:

medium📝 Debug Q14 of 15
Python - While Loop
Find the error in this nested while loop code:
i = 0
while i < 2:
    j = 0
    while j < 2:
    print(i, j)
    j += 1
    i += 1
ANo error, code runs fine
BInner loop print and increments are not indented properly
CVariables i and j are not initialized
DOuter loop condition is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation inside inner while loop

    The print and j increment lines must be indented inside the inner while loop to run repeatedly.
  2. Step 2: Identify effect of wrong indentation

    Without indentation, print and j += 1 run only once, causing an infinite loop or logic error.
  3. Final Answer:

    Inner loop print and increments are not indented properly -> Option B
  4. Quick Check:

    Indent inner loop body correctly [OK]
Quick Trick: Indent inner loop body to avoid logic errors [OK]
Common Mistakes:
MISTAKES
  • Forgetting to indent inner loop code
  • Incrementing outer loop variable inside inner loop
  • Misplacing loop conditions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes