简体   繁体   中英

Trouble Understanding the Recursion Here

Can someone please help to explain to me why this ends up in an infinite recursion loop?

The variable length reaches the value 1 but for some reason the loop is still entered even though the loops condition is while (length>1).

I've tried printing values and running it over and over again, maybe I'm missing something more obvious or someone can explain this more simply. Thanks.

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) { 
    while (length > 1) {
        System.out.print((length - 1) + " ");
        xMethod(length - 1);
    }
}

Additional info.

When I dubugged this code :

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) { 
    while (length > 1) {
        System.out.print((length - 1) + " ");
        xMethod(length - 1);
    }
    System.out.println("Coming out of while");
}

Below is the output :

4 3 2 1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
//repeated Infinite times

After coming out of while loop why it is going back in the same while loop with length as 2?

Edit: I appreciate all of your responses and understand that if I wanted to code something like this I would probably us an if statement as most recursive methods do but this is simply a question of me perhaps not understanding how the scope or call stack works. If I'm correct the while loop block holds on to the value of length as 2 no matter what happens outside of that block?

Because you are not updating the value of length in the current method. The value is just decremented when sending to the method.

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) {
    while (length > 1) {
        System.out.print((length) + " ");
        xMethod(length);
        length--;
    }
}

The variable length never reaches the value 1 in any loop, you mix two design and i think you need on of them, recursive method or loop.

first design:

public static void main(String[] args) {
    xMethod(5);
}
public static void xMethod(int length) { 
    System.out.print((length - 1) + " ");
    if(length > 1)
        xMethod(length - 1);
    }
}

another way:

public static void main(String[] args) {
    xMethod(5);
}
public static void xMethod(int length) {
    while (length > 1) {
        System.out.print((length--) + " ");
    }
}

you can select one of these, it depend on your design. if it's not your answer, please write your expect output.

You are doing 2 things here. When you are writing a recursive code, you always need to think when the when code will end. Your code does not have a end case.

public static void main(String[] args) {
             xMethod(5);
}

public static void xMethod(int length) { 

     System.out.println("Method Start "+ length);
        while (length > 1) {

            System.out.println("Inside while "+ length);

             xMethod(length - 1);
        }
        System.out.println("Method End "+ length);                 
    }
}

Now this code produces the following output:

Method Start 5
Inside while 5
Method Start 4
Inside while 4
Method Start 3
Inside while 3
Method Start 2
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
.
.

As you can clearly see,

Inside while 2
Method Start 1
Method End 1

is repeated again and again.

So what this means is, when the length is 2, the following will happen.

while (2 > 1) {
     System.out.println("Inside while "+ length);
     xMethod(1);
}

The output for this is

Inside while 2

Now, xMethod(1) doesn't even enter the while loop, so this will be printed.

Method Start 1
Method End 1

But you should now understand that while(2>1) is again execute because the length has not changed and it is still 2 .

while (2 > 1){
    System.out.println("Inside while "+ length);
    xMethod(1);
}

goes on and the loop continues.

because when length reaches 2 xMethod(2) is called and therefore xMethod(1) follows, when xMethod(1) ends, since length is still 2 it again calls xMethod(2) and the this calls xMethod(1) it repeates..

to fix it, use return after xMethod(length - 1);

public static void main(String[] args){
        xMethod(5);
    }

    public static void xMethod(int length) { 

        while (length > 1) {

            System.out.print((length - 1) + " ");

             xMethod(length - 1);
             return;
        }
        System.out.println("Coming out of while");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM