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 →Concept understanding — Infinite Loop
Infinite Loop — The Intuition
Imagine you are walking along a straight road. You keep walking, and the road never ends. There is no last step, no final destination. That is the basic idea of an infinite loop — a sequence that never stops.
But here is the crucial twist: an infinite loop is not just "very long." It is endless. No matter how many steps you take, there is always another step waiting. The loop has no final element, no last iteration.
The Precise Statement
In programming and mathematics, an infinite loop is a sequence of instructions that repeats indefinitely because the condition for termination is never met — or because no termination condition exists at all.
Formally, consider a loop defined by:
- An initial state
- A transition function that maps the current state to the next state:
- A termination condition that, when true, stops the loop
The loop is infinite if for every , is false. That is, the condition that should stop the loop never becomes true, no matter how many times the loop body executes.
An infinite loop is not the same as a loop that runs a very large but finite number of times. If the loop will eventually stop (even after a billion iterations), it is finite. An infinite loop cannot stop.
A Simple Example
x = 1
while x > 0:
x = x + 1
Here, starts at 1 and increases by 1 each time. Since is always positive, the condition x > 0 is always true — the loop never ends. This is an infinite loop.
Contrast with:
x = 1
while x > 0:
x = x - 1
Here, eventually becomes 0, the condition x > 0 becomes false, and the loop stops. This is a finite loop.
Why It Matters
Infinite loops are not just a curiosity. They are:
- A bug when unintended — your program freezes or hangs …
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.