Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public class Recursion {
    public int  i=0;
    public static void main(String[] args){
      Recursion s = new   Recursion();
      s.increment();
    }

    public  void increment(){
       System.out.println("Start of the function"+i);
        while(i<3) {
            i++;
            increment();
            System.out.println("Inside while loop"+i);
        }
        System.out.println("OUT of While loop "+i);
        i=0;
    }
}

OUTPUT
...
Start of the function1
Start of the function2
Start of the function3
OUT of While loop 3
Inside while loop0
Start of the function1
Start of the function2
Start of the function3
OUT of While loop 3
Inside while loop0
Start of the function1
Start of the function2
Start of the function3
OUT of While loop 3
Inside while loop0
Start of the function1
Start of the function2...
Exception in thread "main" java.lang.StackOverflowError

What I have tried:

When i use break statement at the end of while function then the function works as expected without any infinite loop.
Output
Start of the function0
Start of the function1
Start of the function2
Start of the function3
OUT of While loop 3
Inside while loop0
OUT of While loop 0
Inside while loop0
OUT of While loop 0
Inside while loop0
OUT of While loop 0


My doubt here is 1) why function is called recursively (infinite loop), even after coming out of while loop and at the end of increment block statement also?

2) If i dont use break statement why the entire
increment()
function is getting executed again and again, instead of executing only Sysout("Inside while loop") as done for break statement ?
Posted
Updated 21-Nov-20 7:09am
v2
Comments
KarstenK 21-Nov-20 13:10pm    
Are you serious? Have you used the debugger once? :-O

Do yourself a favor and remove wrong language tags.
Quote:
Why does WHILE loop without break statement in recursion function works like infinite loop ?

This is what you requested in your code. The best to understand what you did, is to watch your code perform.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
It's pretty obvious when you see it, and if you'd had a quick look with the debugger you would have seen it too.

What does increment do with i when it exits the while loop?
It sets it to zero. So when it returns to a previous iteration of the function, it's back in the loop, but now i is zero, so it goes round the loop again and calls increment recursively.

Seriously: use the debugger to follow what is going on and it's really obvious that you can't ever get out of the loop until the stack is exhausted.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900