A Loop Needs More Than Repetition
Consider:
WHILE n > 0
n = n - 1
END WHILE
If \(n\) begins as a nonnegative integer, every repetition decreases \(n\) by \(1\). Eventually,
\[ n=0, \]and the condition becomes false.
Now compare:
WHILE n > 0
n = n + 1
END WHILE
For positive \(n\), the process moves away from \(0\). The stopping condition exists, but the algorithm does not move toward it.
\[ \boxed{ \text{termination} = \text{stopping condition} + \text{guaranteed progress toward stopping} } \]What Is Termination?
An algorithm terminates on an input when it finishes after a finite number of steps.
If it terminates for every valid input in its intended domain, we say the algorithm is terminating on that domain.
Termination is different from correctness.
Terminates but Wrong
A procedure can stop quickly and still return an incorrect answer.
Correct Idea but Never Stops
A procedure can describe a valid target but fail to reach a stopping state.
A useful algorithm generally needs both:
\[ \boxed{ \text{correctness} + \text{termination} } \]What Is a Stopping Condition?
A stopping condition is a rule that determines when a repeated or recursive process should end.
Examples:
\[ n=0, \] \[ |x_{k+1}-x_k|<10^{-6}, \] \[ i>100, \] \[ \text{list is empty}. \]The stopping condition defines a state in which further repetition is no longer required.
Loop Guards
A loop guard is the condition that controls whether a loop continues.
For example:
WHILE x >= 10
x = x / 10
END WHILE
The guard is
\[ x\geq10. \]The loop continues while the guard is true and stops when it becomes false.
So the stopping condition is equivalent to
\[ x<10. \]A Terminating Loop Structure
Progress toward Termination
A terminating process usually has some quantity that moves steadily toward a limit.
Examples:
- a counter increases toward a fixed maximum,
- a positive integer decreases toward \(0\),
- an interval width shrinks toward a tolerance threshold,
- a recursive input becomes structurally smaller.
The crucial question is:
What changes every repetition that makes endless continuation impossible?
Variants and Ranking Functions
A variant or ranking function is a quantity used to prove that a process cannot continue forever.
A common pattern is:
- The variant is always a nonnegative integer.
- Each repetition makes it strictly smaller.
Then infinite repetition is impossible because there is no infinite strictly decreasing sequence of nonnegative integers.
Example
WHILE n > 0
n = n - 2
END WHILE
For nonnegative integer input, a convenient variant is
\[ V(n)=\max(n,0). \]While the loop continues, \(n\) decreases by \(2\), so the process reaches \(n\leq0\) after finitely many steps.
Fixed-Count Termination
A fixed-count loop has termination built into its repetition count.
FOR i = 1 TO 100
perform action
END FOR
Assuming each individual iteration finishes, the loop body executes exactly \(100\) times.
This makes termination straightforward:
\[ \boxed{ \text{known finite repetition count} \Longrightarrow \text{finite loop} } \]Condition-Controlled Termination
Condition-controlled loops require more reasoning because the number of iterations is not always known in advance.
WHILE x > 1
x = x / 2
END WHILE
For positive \(x\), repeated halving eventually makes
\[ x\leq1. \]The loop count depends on the starting value, but the direction of progress is clear.
Boundary and Off-by-One Conditions
A small change in a stopping condition can change the number of repetitions.
Compare:
WHILE i < 5
with:
WHILE i <= 5
If \(i\) starts at \(1\) and increases by \(1\), the first processes
\[ 1,2,3,4, \]while the second processes
\[ 1,2,3,4,5. \]These are off-by-one differences.
Boundary conditions should be checked deliberately, especially at the first and last valid states.
Recursive Termination
Recursive algorithms need:
- a base case,
- a recursive call that moves toward the base case.
Terminating Example
COUNTDOWN(n)
IF n = 0
RETURN
END IF
COUNTDOWN(n - 1)
END
For nonnegative integer \(n\), every call reduces the input.
Nonterminating Direction
COUNTDOWN(n)
IF n = 0
RETURN
END IF
COUNTDOWN(n + 1)
END
For positive \(n\), the calls move away from the base case.
Stopping Approximation Methods
Approximation algorithms often stop when a numerical criterion is met.
Examples:
\[ |x_{k+1}-x_k|<\varepsilon, \] \[ |f(x_k)|<\varepsilon, \] \[ U_k-L_k<\varepsilon. \]Here \(\varepsilon>0\) is a requested tolerance.
These conditions are useful only when they are connected to the quality of the approximation.
For example, bisection guarantees that interval width shrinks by half each iteration, so a width-based stopping condition has a direct error meaning.
Maximum-Iteration Safeguards
A practical algorithm may include a maximum number of iterations:
iteration = 0
WHILE error > tolerance AND iteration < 1000
refine estimate
iteration = iteration + 1
END WHILE
This safeguard prevents an unexpected failure to converge from running indefinitely.
But reaching the maximum does not mean the mathematical goal was achieved.
The algorithm should distinguish:
- success: tolerance reached,
- forced stop: iteration limit reached first.
Termination and the Input Domain
An algorithm may terminate for some inputs but not others.
Example
WHILE n != 0
n = n - 2
END WHILE
If \(n\) starts as a positive even integer, the loop reaches \(0\).
If \(n\) starts as a positive odd integer, the states are
\[ 5,3,1,-1,-3,\ldots \]and \(n\) never equals \(0\).
So termination depends on the stated domain.
\[ \boxed{ \text{termination claims must name the inputs for which they hold} } \]Proving Termination
A basic termination proof often follows this structure:
- Identify a progress measure.
- Show that it changes strictly in the required direction each repetition.
- Show that it cannot change that way forever.
- Conclude that the stopping condition must eventually be reached.
Example
Consider:
WHILE n > 0
n = n - 1
END WHILE
For nonnegative integer \(n\):
- variant: \(n\),
- while the loop runs, \(n>0\),
- each iteration replaces \(n\) by \(n-1\),
- nonnegative integers cannot decrease forever.
Therefore the loop terminates.
Nontermination and Infinite Processes
Not every infinite process is a programming mistake.
Mathematics intentionally studies infinite:
- sequences,
- series,
- continued processes,
- dynamical systems.
The distinction is purpose.
An algorithm intended to return a finite answer must have a finite completion rule.
An infinite mathematical process may instead be studied through limits, convergence, or long-run behavior.
Practical Applications
Software
Loops need reliable exit conditions so programs do not hang indefinitely.
Numerical Analysis
Approximation methods stop when an error, residual, interval width, or iteration limit meets a stated rule.
Data Processing
Algorithms terminate when all records or list elements have been processed.
Search
A search may stop when the target is found or when no candidates remain.
Recursive Structures
Tree and folder traversal terminate when leaf nodes or empty substructures are reached.
Worked Examples
Example 1: Fixed Count
FOR i = 1 TO 8
output i
END FOR
The loop executes exactly
\[ \boxed8 \]times.
Example 2: Decreasing Integer
WHILE n > 0
n = n - 3
END WHILE
For \(n=10\):
\[ 10,\ 7,\ 4,\ 1,\ -2. \]The loop stops because \(-2\) does not satisfy \(n>0\).
Example 3: Off-by-One Difference
Start with \(i=1\).
WHILE i < 4
output i
i = i + 1
END WHILE
Output:
\[ \boxed{1,2,3}. \]Using \(i\leq4\) instead would also include \(4\).
Example 4: Recursive Termination
\[ S(0)=0, \] \[ S(n)=n+S(n-1). \]For nonnegative integer \(n\), each call reduces the argument by \(1\), so the process reaches \(S(0)\) after finitely many calls.
Example 5: Bisection Stopping
Suppose the initial interval width is \(4\), and we want width below \(0.01\).
After \(n\) bisections:
\[ \frac{4}{2^n}<0.01. \]Since
\[ 2^9=512, \qquad \frac4{512}\approx0.0078125, \]nine bisections are sufficient.
Example 6: Domain-Dependent Failure
WHILE n != 0
n = n - 2
END WHILE
For \(n=6\), the states are
\[ 6,4,2,0, \]so the loop terminates.
For \(n=5\), the states are
\[ 5,3,1,-1,-3,\ldots \]so the loop does not terminate.
Common Errors
1. Assuming a Stopping Condition Guarantees Termination
The process must also move toward a state where that condition is reached.
2. Forgetting to Update the Guard Variables
If the variables controlling the loop never change, the loop may run forever.
3. Ignoring the Input Domain
A loop may terminate for even integers but fail for odd integers.
4. Confusing “Stopped” with “Succeeded”
A maximum-iteration limit can stop a process before the desired result is achieved.
5. Missing a Boundary Case
Using \(<\) instead of \(\leq\) can change the final processed value.
6. Assuming Small Changes Mean Convergence
Successive approximations may become close to one another without being close to the desired solution in every method.
7. Treating Infinite Mathematical Processes as Automatically Invalid
Infinite sequences and limits can be mathematically intentional even though a finite-output algorithm needs a stopping rule.
Practice
Termination Check
Does this terminate for positive integer \(n\)?
WHILE n > 0
n = n - 1
END WHILE
Check Answer
Yes. \(n\) decreases by \(1\) and eventually reaches \(0\).
Bad Direction
Does this terminate for positive integer \(n\)?
WHILE n > 0
n = n + 1
END WHILE
Check Answer
No. The process moves farther from the stopping region.
Boundary Count
How many values are output?
i = 1
WHILE i <= 5
output i
i = i + 1
END WHILE
Check Answer
\[ \boxed5 \]values: \(1,2,3,4,5\).
Recursive Progress
Which recursive call moves positive integer \(n\) toward base case \(n=0\)?
A. \(f(n+1)\)
B. \(f(n-1)\)
Check Answer
\[ \boxed{f(n-1)} \]Maximum Iterations
If an approximation method stops because it reaches its iteration limit before meeting the tolerance, should the result automatically be labeled successful?
Check Answer
No. The process stopped, but the success condition was not reached.
The Deeper Idea
Termination is a mathematical statement about process.
A loop or recursive algorithm generates a sequence of states:
\[ s_0,s_1,s_2,\ldots \]To prove termination, we show that these states cannot continue forever under the update rule without reaching a stopping region.
That connects termination to earlier ideas:
- logic defines continuation conditions,
- iteration generates successive states,
- recursion reduces problems toward base cases,
- inequalities define stopping regions,
- approximation uses tolerances as numerical stopping rules.
The deepest practical habit is to ask:
“What quantity proves that this process cannot continue forever?”
Once that quantity is identified, termination becomes something we can reason about rather than merely hope for.
Summary
- An algorithm terminates when it finishes after a finite number of steps.
- A stopping condition defines when repetition or recursion should end.
- A loop guard controls whether another iteration occurs.
- A terminating process needs a form of guaranteed progress toward stopping.
- A variant or ranking function can prove that endless repetition is impossible.
- Fixed-count loops terminate after a known finite number of iterations.
- Condition-controlled loops require analysis of how the state changes.
- Boundary choices such as \(<\) versus \(\leq\) can create off-by-one errors.
- Recursive termination requires a base case and recursive calls that move toward it.
- Approximation methods often stop using error, residual, interval-width, or iteration-count conditions.
- A maximum-iteration safeguard prevents runaway computation but does not guarantee success.
- Termination claims must state the input domain for which they hold.
- A proof of termination identifies a progress measure that cannot decrease or increase in the required direction forever.
- Infinite mathematical processes can be meaningful even when finite-output algorithms require termination.
Stopping is an event. Termination is a guarantee. A well-designed algorithm does not merely contain an exit condition—it changes state in a way that makes reaching that exit unavoidable for every valid input in its intended domain.
Algorithms and Computational Thinking
- Previous: Exact and Approximate Methods
- Algorithms and Computational Thinking — return to the lesson directory.