I would love to know the reasoning behind the function not exiting when the if statement returns false because the printf is within the if's block.
It does exit - but only when the escape condition is met - for all the other calls you are going 'down' another layer - like russian dolls, until you get to the call where x < 5 - then each call is exited succesively back to the original call which exits to main()
maybe this example helps to visualise it, the code used is purely for illustration, I dont recommend writing something this way! ... the opening braces of the while loop are analgous to the recursive calls going 'down' (as i picture it) until the recursion depth limit is met and then the closing braces represent the exits being met and coming back out of the stack
Code:
#include <stdio.h>
void CountDown(int x)
{
while(x >= 5)
{
x -= 5;
printf("%d\n", x);
while(x >= 5)
{
x -= 5;
printf("%d\n", x);
while(x >= 5)
{
x -= 5;
printf("%d\n", x);
while(x >= 5)
{
x -= 5;
printf("%d\n", x);
while(x >= 5)
{
x -= 5;
printf("%d\n", x);
}
}
}
}
}
}
int main()
{
int x = 25;
printf("%d\n", x);
CountDown(x);
return 0;
}