Bird
0
0

Identify the error in this nested loop code:

medium📝 Debug Q6 of 15
C - Loops
Identify the error in this nested loop code:
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++)
printf("%d %d\n", i, j);
i++;
}
AInner loop condition should be j<3 instead of j<2.
BMissing semicolon after printf statement.
CModifying loop variable 'i' inside the inner loop causes unexpected behavior.
DThe outer loop should be a while loop instead of for loop.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop variables

    Variable 'i' is the outer loop counter, 'j' is inner loop counter.
  2. Step 2: Check for modifications inside loops

    Inside inner loop, 'i++' modifies outer loop counter unexpectedly.
  3. Step 3: Understand impact

    Changing 'i' inside inner loop disrupts loop control, causing skipped iterations or infinite loops.
  4. Final Answer:

    Modifying loop variable 'i' inside the inner loop causes unexpected behavior. -> Option C
  5. Quick Check:

    Do not modify outer loop variable inside inner loop [OK]
Quick Trick: Avoid changing outer loop variable inside inner loop [OK]
Common Mistakes:
  • Incrementing outer loop variable inside inner loop
  • Assuming inner loop can safely modify outer loop counter
  • Ignoring loop control variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes