Sir in case of 5 to 1 firstly the value is print and then decrement
In case of 1 to 5 value is decrement and print but if we see the program the value 5 pass as an argument then it will decrement and print i dont no how it will printing the 1 to 5.
Regaring printing 1 to
Hi @saurabhananta,
See in the case of printing from 1 to 5 we have to first make the recursion call and when the base case is hit and a call in stack is about to end before that we print so this will happen as follows … Lets say for printfn(n) where n=5 we will go in program and call printfn(n-1) and then as new call or lets say a new function will start from the first line with n=4 which will again call printfn(n-1) and then a new function will begin with n=3 so like this the code will never hit the next System.out.println(n) line until base case is hit … Lets say base case is hit and n==0 then the function will exit and below the printfn(n-1) call the line system.out.println(n) will be executed with n = 1 and after execution this function will end too … After this function ends we will be in the function with n=2 and then system.out.println(n) with n=2 will execute and we will get a 2 on console.
Please see video again if you don’t understand. Remember that when printfn(n-1) function is called then a completely new function is called from the first line over the current function …