Q.What is an infinite loop? Give one example.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →An infinite (endless) loop is one whose condition never becomes False — typically because the loop variable is never updated — so the body repeats forever, e.g. while i <= 5: print(i) with no change to i.
The idea. A while loop keeps executing as long as its condition evaluates to True. Correct loops guarantee progress towards termination — something inside the body eventually falsifies the condition. When that update is missing (or the condition is written so it can never fail), the loop becomes infinite: the program appears to hang or floods the screen, and must be interrupted from outside (Ctrl+C in the shell, or Restart Shell in IDLE).
Example:
i = 1
while i <= 5:
print(i) # forgot: i = i + 1
1
1
1
1
... (never stops — interrupt with Ctrl+C)
Because i stays 1, the test 1 <= 5 is True on every pass. One added line fixes it:
i = 1
while i <= 5:
print(i)
i = i + 1 # now the loop ends after printing 1 2 3 4 5
Common causes of accidental infinite loops:
- Forgetting the update statement (as above).
- Updating in the wrong direction (i = i - 1 when the test is i <= 5 and i starts at 1).
- A continue placed before the update line in a while loop, so the update is skipped. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.