Bird
0
0

You want to print all numbers from 0 to 9 except 5, but stop printing if the number reaches 8. Which code snippet correctly uses break and continue?

hard📝 Application Q15 of 15
Python - Loop Control
You want to print all numbers from 0 to 9 except 5, but stop printing if the number reaches 8. Which code snippet correctly uses break and continue?
Afor i in range(10): if i == 5: break if i == 8: continue print(i)
Bfor i in range(10): if i == 5: continue if i == 8: break print(i)
Cfor i in range(10): if i == 8: continue if i == 5: break print(i)
Dfor i in range(10): if i == 5: continue if i == 8: continue print(i)
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirements for skipping and stopping

    We skip printing 5 (use continue) and stop printing at 8 (use break).
  2. Step 2: Check each option for correct order and logic

    for i in range(10): if i == 5: continue if i == 8: break print(i) correctly continues at 5 to skip it, breaks at 8 to stop loop, and prints others.
  3. Final Answer:

    Option B code snippet -> Option B
  4. Quick Check:

    continue skips 5, break stops at 8 [OK]
Quick Trick: Continue skips unwanted, break stops loop early [OK]
Common Mistakes:
MISTAKES
  • Swapping break and continue conditions
  • Using break to skip instead of continue
  • Not stopping loop at 8

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes