Bird
0
0

Which of the following is the correct syntax to prevent an infinite loop in Python?

easy📝 Syntax Q3 of 15
Python - While Loop
Which of the following is the correct syntax to prevent an infinite loop in Python?
Awhile i < 5: print(i) i += 1
Bwhile i < 5 print(i) i += 1
Cwhile i < 5: print(i) i += 1
Dwhile i < 5: print(i) i = i - 1
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax correctness

    while i < 5: print(i) i += 1 uses correct indentation and colon after while condition.
  2. Step 2: Verify loop variable update

    while i < 5: print(i) i += 1 increments i, moving towards exit condition, preventing infinite loop.
  3. Final Answer:

    while i < 5: print(i) i += 1 -> Option A
  4. Quick Check:

    Correct syntax + variable update = prevent infinite loop [OK]
Quick Trick: Indent and update loop variable properly [OK]
Common Mistakes:
MISTAKES
  • Missing colon after while condition
  • Incorrect indentation
  • Not updating loop variable or updating wrongly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes