I know that I'm almost 13 years late to the party, but I ran into this problem in a quiz at my college, I answered it wrong and I couldn't figure out why it's behaving like this. So I took some time and I believe I can explain it in a way that would make sense to everyone.
Iteration #1. of the while loop declared inside the method call strangeMethod(5);
public static void strangeMethod(5) {
while (5> 1) {
System.out.println(4);
strangeMethod(4);
}
}
Because 5 is greater than 1, the code within the while block got executed, so, now we can see the print statement in the console, but what's really important for you to notice here, is that we did not run a second iteration of this while loop, in fact, we did not even finish the first iteration, it got paused midway so that it can execute the recursive call of strangeMethod(4), and it will never get a chance to finish
To make sure we're on the same page, your answer to the question "how many iterations of the while loop executed in the call strangeMethod(5); completed?" should be not even one iteration.
BUT, we did call strangeMethod(4); now this is a fresh method call, the while loop within it will start to execute since its continuation condition is met, 4 is greater than 1, BUT, just as the previous call, it will not even finish a single iteration and it will call strangeMethod(3);
Iteration #1. of the while loop declared inside the method call strangeMethod(4);
public static void strangeMethod(4) {
while (4 > 1) {
System.out.println(3);
strangeMethod(3);
}
}
Iteration #1. of the while loop declared inside the method call strangeMethod(3);
public static void strangeMethod(3) {
while (3 > 1) {
System.out.println(2);
strangeMethod(2);
}
}
Iteration #1. of the while loop declared inside the method call strangeMethod(2);
public static void strangeMethod(2) {
while (2 > 1) {
System.out.println(1);
strangeMethod(1);
}
}
Now, the call of strangeMethod(2); is different, because it is the only call of strangeMethod() that will actually get to iterate more than once.
You see, the continuation condition for the while loop is, if 2 is greater than 1 which will always be true, just like all the previous call have been. The difference in this method is that the strangeMethod(1); call will not interrupt and pause the while loop, it will start executing, see that the continuation condition while (1 > 1) is not met, and since there is no other code in the call of strangeMethod(1); for it to execute, strangeMethod(1); will actually finish, allowing us to proceed from where we left off, which is to finish iteration #1 of strangeMethod(2); start iteration #2 of strangeMethod(2); check that the continuation condition for the while loop (2>1) still holds true, execute the print statement, execute the strangeMethod(1); call which will finish, and allows us to start iteration #3 of strangeMethod(2); and since we have no base case for when len == 1, strangeMethod(2); will keep iterating indefinitely.
So, the call of strangeMethod(2); is unique in that all the code to be executed within it will actually get executed, allowing the while loop within it to iterate.
Iteration #2. of the while loop declared inside the method call strangeMethod(2);
public static void strangeMethod(2) {
while (2 > 1) {
System.out.println(1);
strangeMethod(1); //Is now just some dead code
}
}
Iteration #3. of the while loop declared inside the method call strangeMethod(2);
public static void strangeMethod(2) {
while (2 > 1) {
System.out.println(1);
strangeMethod(1); //Is now just some dead code
}
}
Now, if this is still not creating that AHA! moment, consider doing the following, adjust the code by adding a simple if block
public static void strangeMethod(2) {
if (len == 1) {
System.out.println(
"strangemethod(1) was executed and finished,
The while loop within it did not run
because its condition was not met
Thus, allowing us to move on to the second
Iteration of the while loop within
strangeMethod(2);"
);
}
while (2 > 1) {
System.out.println(1);
strangeMethod(1);
}
}
OR
public static void strangeMethod(2) {
if (len == 1) {
System.exit(0); // which will end all the previous
// method calls, unlike return; which will
// only end strangeMethod(1); but keep
// strangeMethod(2) still active and
// running
}
while (2 > 1) {
System.out.println(1);
strangeMethod(1);
}
}
For anyone who finds my answer helpful, I would greatly appreciate it if you'd up-Vote it.