Bird
0
0

How can you modify nested while loops to print only pairs where the sum of loop variables is even?

hard📝 Application Q9 of 15
Python - While Loop
How can you modify nested while loops to print only pairs where the sum of loop variables is even?
Example: print (i, j) only if i + j is even.
AAdd an if condition inside inner loop: if (i + j) % 2 == 0: print(i, j)
BAdd if condition outside both loops
CChange inner loop condition to j % 2 == 0
DUse break statement when sum is odd
Step-by-Step Solution
Solution:
  1. Step 1: Use condition inside inner loop

    Check if sum of i and j is even inside inner loop before printing.
  2. Step 2: Why inside inner loop?

    Because each pair (i, j) must be tested individually during iteration.
  3. Final Answer:

    Add an if condition inside inner loop: if (i + j) % 2 == 0: print(i, j) -> Option A
  4. Quick Check:

    Filter pairs inside inner loop with if condition [OK]
Quick Trick: Use if inside inner loop to filter pairs [OK]
Common Mistakes:
MISTAKES
  • Placing condition outside loops
  • Changing loop conditions incorrectly
  • Using break instead of condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes