I have came across a concept in recursion which says that when loops are compiled or interpreted then they get converted to recursive functions. If it is true how it takes place ?
3
It depends. In many language implementations, using recursion for loops is a good way to cause a Stack Overflow as each call adds more and more entries to the stack until it eventually runs out. Some implementations get around that by taking advantage of their design combined with the Tail Call Optimization, which is effectively reusing the current stack entry because you know it will no longer be needed.
That though is rare. Most implementations simply convert the loop into properly positioned goto
statements (or jump
instructions depending on your perspective) so instructions are repeated.
And certain implementations in certain situations will do an optimization called Loop Unrolling where they effectively copy the code for the number of iterations of the loop rather than do either of these options. That though generally requires the number of iterations to be known at compile time and is impractical for big loops (either in instructions or iterations).
3