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..
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.
来源:https://stackoverflow.com/questions/3070693/recursion-inside-while-loop-how-does-it-work