0

Could someone help me trace this recursive print statements to help me understand Recursion.

public static void printStuff(int Level) {
   if (level == 0) {
      System.out.print("*");
      } else {
      System.out.print("[");
      printStuff (level-1);
      System.out.print(",");
      printStuff  (level-1);
      System.out.print("]")
   }
}
public static void main (String [] args) {
   printStuff(2);
}
  • 4
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) - If you want a step by step description that walks you through what that code exactly does I'd recommend using a debugger. – OH GOD SPIDERS Nov 09 '21 at 13:06
  • 1
    To understand recursion you might want to read articles on it, e.g. [this](https://www.baeldung.com/java-recursion). Basically, you encounter recursion if a method calls itself like `printStuff(...)` is doing. There can be longer "cycles" like `A->B->C->A->B->C...`, i.e. a method can call itself indirectly. Also, to avoid infinite recursion you need some condition when _not_ to do any further recursive calls, like `if (level == 0)` in your example. – Thomas Nov 09 '21 at 13:13
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Yarek T Nov 11 '21 at 08:40
  • No, that does not help Yerek. I know how to use a debugger. The point of the assignment was to trace code that is on paper (no IDE involved), without using an ide or debugger. – Larry Drifke Nov 12 '21 at 12:04

0 Answers0