Comment on Why does the for loop repeat in this recursion?

Merwyn@sh.itjust.works ⁨9⁩ ⁨months⁩ ago

You are looking at a recursive method, as you can see with the line draw(n-1). You can search for “recursive function” on internet for a better understanding.

Basically, the method draw is called a first time n = a user input, but then this method call itself with n-1 until it reach 0. So you can think as if function draw(6) will call draw(5) and wait for it to return before continuing, draw(5) call draw(4), ect until draw(0) that return immediately.

So then the order of execution will be draw(1) that print " #\n" and return, then draw(2) will proceed to print “##\n” and return, then draw(3), ect until draw(n).

source
Sort:hotnewtop