Bounded recursion
What you will learn
Recursion occurs when a method calls itself, directly or indirectly. Each call handles a smaller problem until a base case stops further calls. Recursion is useful for naturally nested or self-similar structures, but a loop is often simpler for ordinary counting.
public class Main { static int sumTo(int n) { if (n == 0) { return 0; } return n + sumTo(n - 1); } public static void main(String[] args) { System.out.println(sumTo(3)); System.out.println(sumTo(0)); }}Expected output:
60At zero the method returns without another call; the pending additions produce six.
How it works
For sumTo(n) with n from 0 to 10, the base case returns 0 when n is zero. Otherwise the method returns n plus sumTo(n - 1). The argument decreases toward zero; each invocation has its own parameter. For n = 3, the calls descend 3, 2, 1, 0, then return 0, 1, 3, 6. The pending additions are completed as the calls return. A base case alone is insufficient if some permitted input cannot reach it. Each active call requires execution-frame resources; Java does not guarantee tail-call optimization. Excessive call depth can produce StackOverflowError. Keep this chapter within stated small input bounds rather than experimenting with unbounded recursion. Use a loop for simple totals when it is clearer.
public class Main { static void trace(int n) { if (n == 0) { return; } System.out.println("Enter " + n); trace(n - 1); System.out.println("Leave " + n); } public static void main(String[] args) { trace(2); }}Expected output:
Enter 2Enter 1Leave 1Leave 2The second print waits until the smaller invocation returns.
Check a boundary or unusual input
public class Main { static int sumTo(int n) { int total = 0; for (int i = 1; i <= n; i++) { total += i; } return total; } public static void main(String[] args) { System.out.println(sumTo(3)); System.out.println(sumTo(0)); }}Expected output:
60The loop produces the same result for the specified bounded domain without accumulating recursive invocations.
Common mistakes
Do not call sumTo(n) again with unchanged n or use a negative input with a zero-only base case. Both may prevent termination. Do not assume recursion is always faster or always uses less memory. Document the accepted input range; arithmetic overflow is a separate issue even if recursion terminates.
Try it yourself
Implement static int factorial(int n) for 0–6, where 0! = 1 and n! = n times (n-1)!. Use recursion for this exercise. Test 4, 0, 1 and 6. Do not call with out-of-range values.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounded recursion — knowledge check
Apply bounded recursion to examples and boundary conditions.
What is the base case for factorial of nonnegative n?
What ensures sumTo(n - 1) reaches zero for positive small n?
What is sumTo(3) with base 0 and recurrence n + sumTo(n-1)?
Does Java guarantee elimination of tail-recursive calls?
When is a loop often preferable?