Commit e2242bb
committed
feat: Add RecursionIsAwsome class with controlled recursive greeting
WHAT the code does:
- Defines a `RecursionIsAwsome` class.
- `sayHi(int n)` method:
- **Base case**: if `n == 0`, prints `"Done!"` and stops recursion.
- **Recursive case**: prints `"Hi"`, decrements `n`, and calls itself again.
- `main()` calls `sayHi(5)` to demonstrate.
WHY this matters:
- Shows proper use of **recursion with a base case**.
- Prevents infinite recursion by stopping when `n == 0`.
- Demonstrates decrementing arguments across recursive calls.
HOW it works:
1. `sayHi(5)` → prints `"Hi"` → calls `sayHi(4)`.
2. Continues printing `"Hi"` until `n == 0`.
3. At `n == 0`, prints `"Done!"` and terminates.
Tips & gotchas:
- Works only for non-negative values of `n`.
Negative values won’t trigger the base case properly.
- Recursion depth depends on `n`; large `n` may cause `StackOverflowError`.
- Could also be written iteratively with a loop, but recursion is great for learning.
Use-cases:
- Educational demo of **recursion with termination condition**.
- Teaching difference between infinite recursion and controlled recursion.
- Can be adapted to countdowns, repeated tasks, or recursive algorithms.
Short key: algo-recursion-sayhi-basecase.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6f7ea86 commit e2242bb
File tree
1 file changed
+2
-4
lines changed- Section10Methods/RecurssionPractice/src
1 file changed
+2
-4
lines changedLines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | | - | |
| 6 | + | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
| |||
0 commit comments