Recursion inside while loop, How does it work?

北战南征 提交于 2019-12-05 15:23:38

That'll print 4 3 2 1 1 1 1 1 1...

And get stuck in a loop because nothing ever modifies len in the scope of the while loop. The first calls (with len=5, 4, then 3) go through one loop iteration, and are left waiting for Strangemethod to return. When when len=2, the while loop calls strangemethod(1), and since len is not greater than 1, the while loop finishes and that call returns. But len is still 2 in the bottom-most remaining whle loop, so it calls strangemethod(2) again. And again. And again.

if() would've been more appropriate than while().

If I'm not mistaken, isn't this causing an infinite loop?

Once strangemethod(1) returns the strangemethod(2) would print 1 again and then call strangemethod(1) again.

Are you forgetting to decrement len after the strangemethod call?

EDIT :SORRY For the first reply didnt realise it..it will cause an infinite loop

Here is a simple flow - for e.g len =5

 public static void Strangemethod(5) {
            while(5 > 1){
                System.out.println(5-1);
                Strangemethod(5 - 1);
            }
public static void Strangemethod(4) {
            while(4 > 1){
                System.out.println(4-1);
                Strangemethod(4 - 1);
            }
public static void Strangemethod(3) {
            while(3 > 1){
                System.out.println(3-1);
                Strangemethod(3 - 1);
            }
    public static void Strangemethod(2) {
            while(2 > 1){
                System.out.println(2-1);
                Strangemethod(2 - 1);
            }
    public static void Strangemethod(1) {
            while(1 > 1){//goes back to original(above) call and then an infinite loop since len was never  decremented

            }

Prints 4 3 2 1 1.....

You don't say what you expect the code to do. However, the obvious point of note that the len variable does not change value within the Strangemethod method - it could have been declared final. Possibly what you wanted to do was decrement it with --len; (equivalent to len = len - 1;).

Try adding len--; after Strangemethod(len - 1);. It won't send you into an infinite loop then. Alternatively, you could do

System.out.println(--len);
Strangemethod(len);

This code will loop forever.

The result of len - 1 is never stored in the while loop so it can't exit and when len = 2 it'll just sit there outputting 1s.

It's unusual to use a while in recursive functions. I'd typically expect to see an if in its place, which would give you the output:

4
3
2
1

If you really do need the while then I'd rewrite the loop like this:

while(len > 1)
{
  len--;
  System.out.println(len);
  Strangemethod(len);
}

This will output:

4
3
2
1
1
2
1
1
3
2
1
1
2
1
1

Also, I think someone should point out that len is never decremented so you get an infinite loop. I see that only 7 people have mentioned that so far.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!