+ 1
JAVA How this code work?
Isnt this code output 234 only? Why 2344 https://code.sololearn.com/cE9V6TM7EnmQ/?ref=app
6 ответов
+ 7
For each iteration of the recursive function, it returns a value that increments every iteration if that value is less than 4 (the value of f). At the start, the function returns 2 from the else clause, then returns 3 from the else clause, and lastly 4, once again from the else clause. At this point, the value of i is 5, and because it is greater than the value of f, it returns f as a string, printing out the second 4 within the output
+ 4
Rael
Well, because the test function is returning a string, you can look at it as if it were just a regular value being added to i. In a way, return (i + test(i+1,f)) can be looked at as return (2 + test(2+1,f)), which will lead to this:
return (2 + (3 + test(4,f))),
return (2 + (3 + (4 + test(5,f)))),
and lastly:
return (2 + (3 + (4 + ("" + 4))))
+ 2
Whoops! Sorry, just fixed my response haha
0
Faisal the i became 5 was from
return (i + test(i+1,f)) ?
return(2 + test(3,4))?
2+3 = 5
if yes,i wanna ask why the "f" doesnt involve in the plus?
0
its 2344
0
nice!