Skip to content

Commit e2242bb

Browse files
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

1 file changed

+2
-4
lines changed

Section10Methods/RecurssionPractice/src/RecursionIsAwsome.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
public class RecursionIsAwsome {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
sayHi(5);
54
}
65
public static void sayHi(int n) {
7-
//Need a Base Case.
8-
//Base case means when to stop. printing infinite times.
6+
//Need a Base Case. Base case means when to stop. printing infinite times.
97
if(n==0) {
108
System.out.println("Done!");
119
}

0 commit comments

Comments
 (0)