-3

Ok so I have a recursive code from my instructor. I understand how the stack functions (first in last out, last in first out, etc). What I don't get is how the instructor was able to print the popping off of the stack without telling the computer to do so. For example, how does this code displaying the popping without the a command to do so?

Here is the code:


public class Recursion{

    public static void main(String [] args){
        printParam(1);
    }
    
    
    public static void printParam(int i) {
        System.out.println("i = " + i);
        
        if(i < 10) {
            printParam(i + 1);
        }
        
        System.out.println("i = " + i);
        
    }



}

enter image description here

Looking at the code it looks like it should top at ten, end the program, and trash the stack.

Louman127
  • 1
  • 2
  • 1
    What do you mean? The command *is* there: the first `System.out.println("i = " + i);` will print `i`, then recursively call itself and add 1 to `i`. Which then reaches the first `System.out.println("i = " + i);` and print `i`, then recursively call itself and add 1 to `i`... etc. And once `i` is `10` it will *not* recursively call itself but reach the *second* `System.out.println("i = " + i);`, finish the function then go back to the caller, go to the second `System.out.println("i = " + i);`, finish the function then go back to the caller...etc. – VLAZ Oct 26 '22 at 13:16
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Oct 26 '22 at 13:17
  • It's not clear where your confusion comes from. The method is invoked 10 times. The method contains 2 statements which print a line of output. There are 20 lines of output. That math seems to add up... – David Oct 26 '22 at 13:18
  • I guess I must need to stare at it for longer. I just don't see how it is continuing once i =10; When it hits ten nothing else should be invoked. To me it looks like it should stop once the 10 is printed in the first Sys out, then if (i < 10) will not be invoked, the sys out showing 10 again. After the second 10 is printed, I still don't see at what point it is popping off 9, 8, 7. – Louman127 Oct 26 '22 at 13:27
  • "*When it hits ten nothing else should be invoked*" No, it just won't recursively call the method any more. The rest of the code will still run. The `if` is not the end of the method. If you have `method1 () { System.out.println("before"); method2(); System.out.println("after"); }` the "after" will still be printed after `method2()` completes. Recursion changes none of this, it's just that you're calling `method1()` again. The code after will still run after the recursive call finishes. – VLAZ Oct 26 '22 at 13:34
  • So at what point does i =9 after the Sys out i = 10 is printed – Louman127 Oct 26 '22 at 14:26
  • @Louman127: Forget for a moment that the method is recursive. Suppose that within the `if` block it was calling some other method. When that other method completes whatever it does, do you expect this method to stop executing or do you expect it to continue to the second `System.out.println` statement after the `if` block? What behavior do you expect in that scenario and why? – David Oct 26 '22 at 14:55
  • Ok I think I might be wrapping my head around this. Basically, the method never was able to finish because it kept being recursively invoked? Then once it stops stacking the print method (when 10 =10), because of recursive invocations, it is then able to finish and it prints the sys outs ( the sys out after checking the condition) that were not able to be printed because it was "skipped" due to printParam() being invoked recursively? – Louman127 Oct 26 '22 at 15:15
  • @Louman127: You're overthinking it. Just look at the method. What does it do? (1) Print a line of output. (2) Conditionally call a method. (3) Print another line of output. *That's all.* The method it calls happens to be the same method, but that doesn't matter. And when it calls it, that method call also does those exact three things. Because that's what the method does. Given the logic shown, this method will be called 10 times. Each call will produce a total of 2 lines of output. The result is 20 lines of output. 10 * 2 = 20. – David Oct 26 '22 at 15:24
  • @Louman127: As advised in the beginning, now would be a great time for you to start using a step debugger. When you do that, you can step into each method call, watch what it does, and step back out to the method that called it. By stepping through the code in a debugger you can observe, line by line, what it does. When you do this, either you will observe what the code does and why or you will encounter a *specific* operation which isn't doing something you expected. Which operation was that and why? – David Oct 26 '22 at 15:27
  • Sorry for my noobie mistake. I think I get it now. The first sys outs were printed 10 times due the recursive invocations but the second sys outs were also invoked since it was part of the printParam() method. The second sys outs are simply printing after the the ten recursive invocations. I was looking at this since last night and I guess I was just over thinking it. I'll try other debuggers, the online one I am using (https://www.onlinegdb.com/online_java_debugger) just runs it, no step by step. Thanks for the help. – Louman127 Oct 26 '22 at 15:33

1 Answers1

0

printParam(1);

printParam(); <- invokes itself again

2nd Sys out is "skipped" for this session but added to the stack // but it is still invoked above, just "held"

Once the first forced recursive sys outs (the first sys out) is executed, it still "remembers" that it has 10 "hit me in the faces" to print. showWhatHappens

Louman127
  • 1
  • 2