Skip to content
Exercises · Q4

Q.What is an infinite loop? Give one example.

Yanam CbseNCERTSubjective· 2mImportance★★★★★est
69% · 27/39 Questions
🔒 Locked · start free trial →

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 S0S_0
  • A transition function ff that maps the current state to the next state: Sn+1=f(Sn)S_{n+1} = f(S_n)
  • A termination condition T(Sn)T(S_n) that, when true, stops the loop

The loop is infinite if for every n≥0n \geq 0, T(Sn)T(S_n) is false. That is, the condition that should stop the loop never becomes true, no matter how many times the loop body executes.

Watch out

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, xx starts at 1 and increases by 1 each time. Since xx 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, xx 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.