Recursive Method Prints 4 Times

后端 未结 4 611
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 04:05

So I am working on learning how to utilize recursion through Java. I have written a simple program that adds all the numbers between 1 and n and it looks to do it\'s job. Wh

相关标签:
4条回答
  • 2021-01-17 04:22

    It is because of the second print statement below the else. On execution the recursive function is called and it doesn't reach the print statement at the bottom. But after n=1 ie the if condition is executed then it return to the function recursive(2-1) and go to the down and return sum (also the print) and return the value of sum to the place where recursive(3-1) is called and so on.. . That is where it prints each sum.

    A Code which prints only the final sum is give below.Print is included in the main to print final answer only I hope this will help.

    public class add {
    
        public static void main(String[] args) {
    
            int sum = recursiveCall(5);
            System.out.println(sum); // this will print 15
        }
    
        public static int recursiveCall(int num) {
    
            int sum = 0;
    
            if(num == 1) {
    
                sum = 1;
                //System.out.println(sum); this will print 1
                return sum;
            }
            else {
    
                sum = recursiveCall(num - 1) + num;
            }
            // System.out.println(sum); //this is the reason for each sum.
            return sum;
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-17 04:27

    I've drawn a sequence diagram, hope this can explain the recursion procedure.

    There is a retrospective procedure for recursion, the next command will be pushed into the call stack before the recursion method is called. So we can simply say System.out.println will be pushed to call stack before recursiveCall, then after the recursiveCall returned the main process will continue with the top command on the stack, that is System.out.println.

    0 讨论(0)
  • 2021-01-17 04:37

    Let me explain why this happens.

    Your second print statement prints all the sum values except for when n = 1. When n = 1 the first print statement will print out the value

    Think about this this way: Every non-void method call will return. If it does not return, it will continue executing until a return statement is reached.

    You say that the second print statement should print only the last sum value. But you see, there are only two places that the method can return - in the if (num == 1) statement, or at the end of the method. The former location is reached only once when n is 1, which means that the other four times the method will return through the return statement at the end. To reach the return statement at the end, the second print must be executed. This means that the second print statement must be executed four times!

    Use a debugger to step through the code step by step. This is the easiest to understand what actually happens.

    0 讨论(0)
  • 2021-01-17 04:42

    When the recursion is stack is full, then it starts emptying itself from the tail. This is the reason your print statement below the if statements starts to get executed. Note: Recursion process will return its value where its called, so if conditions arent satisfied so it will return to that point, but the whole recursion process will keep on continuing until the stack is emptied. Whatever i said is what is called the Tail Recursion.

    0 讨论(0)
提交回复
热议问题