Why does the for loop repeat in this recursion?
I used the debugger to examine this code but not understanding a couple areas.
- Why does the for loop repeat after it exits to print a new line? If it exits the loop, shouldn’t it be done with it?
- Why is n incremented and not i as stated with i++?
<span style="color:#323232;">
</span><span style="color:#323232;">int main(void)
</span><span style="color:#323232;">{
</span><span style="color:#323232;"> int height = get_int("Height: ");
</span><span style="color:#323232;">
</span><span style="color:#323232;"> draw(height);
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;">void draw(int n)
</span><span style="color:#323232;">{
</span><span style="color:#323232;"> if (n <= 0)
</span><span style="color:#323232;"> {
</span><span style="color:#323232;"> return;
</span><span style="color:#323232;"> }
</span><span style="color:#323232;">
</span><span style="color:#323232;"> draw(n - 1);
</span><span style="color:#323232;">
</span><span style="color:#323232;"> for (int i = 0; i < n; i++)
</span><span style="color:#323232;"> {
</span><span style="color:#323232;"> printf("#");
</span><span style="color:#323232;"> }
</span><span style="color:#323232;"> printf("n");
</span><span style="color:#323232;">}
</span>
Add comment