views:

224

answers:

7

Can you please tell me how does this java code work? :

public class Main {
    public static void main (String[] args)  {
        Strangemethod(5);
    }
    public static void Strangemethod(int len) {
        while(len > 1){
            System.out.println(len-1);
            Strangemethod(len - 1);
        }
}
}

I tried to debug it and follow the code step by step but I didn't understand it.

update: sorry I didn't mention that I know the result of this code but just want to know the steps of the execution..

+1  A: 

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.....

Misnomer
But within a single invocation of `Strangemethod`, `len` is not decremented.
Tom Hawtin - tackline
@VJ: That's an awesome answer, thanks for typing it out!
Dean J
... and then the call to `Strangemethod(2)` will loop forever.
Péter Török
Sorry..I didnt realise it overlooked len was never decermented..
Misnomer
It wasn't wrong.... it just didn't address the glaring issue with the function. Still a valid response. :)
Shaded
+1  A: 

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;).

Tom Hawtin - tackline
+12  A: 

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().

dannysauer
Or possibly, decrementing len within the loop, which would produce a sort of tree of values.
Dave Costa
@Dave A triangle.
Tom Hawtin - tackline
@Dave: Strangemethod(5) -> 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 (like Tom said, a triangle)
JAB
If you want that kind of evil, calling strangemethod with len-- (or, even more evil, --len) would've been similarly entertaining. :)
dannysauer
+1  A: 

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);
JAB
might as well change `len - 1` to just `len` and put the `len--` before the recursive call.
Matthew Whited
@Matthew: Indeed, though there are a variety of ways of using `--` here, as shown by the other variant that I just added.
JAB
very true... or course with M.H. asking what the method does.. it it would probably be best to have the len--; on it's own line for now. (You could have fun and even move it to the `while` expression.)
Matthew Whited
Oh yeah, that's true. `while (len-- > 1)`
JAB
+2  A: 

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?

Shaded
A: 

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
Nick Holt
A: 

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.

Jay