Base Case

The base case tells a recursive method when to stop. Otherwise it would keep calling itself forever!

For example, we could add all numbers 1 to n recursively like this:

public static int sum1ToN(int n) { return n + sum1ToN(n-1); }

If we input 3 as our n, this method will take 3, then add 2, then add 1, then add 0, then add -1, then add -2, etc forever (or until the program crashes).

We want to stop adding when n gets to 1. We'd say that our "base case" is n <= 1, and our code might look like:

public static int sum1ToN(int n) { // base case: if (n <= 1) return 1; return n + sum1ToN(n-1); }

Whenever writing a recursive method, be careful not to forget the base case!

Last updated: June 7, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .