Sequence of events within the if/else


#1

hey simple question I’m sure. I’m following the logical sequence of order in if/else statements, one question though that i can’t wrap my head around. in the following statement:

else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFew = numberOfBottles - 1;
printf(“Take one down, pass it around, %d bottles of beer on the wall.\n”, oneFew);
singTheSong(oneFew);
printf(“Put a bottle in the recylcing, %d empty bottles in the bin. \n”, numberOfBottles);
}
}

shouldn’t the console be displaying the words "put a bottle in the recycling come after each iteration of statement? how is it that the recycling print statement shows up at the end of the statement instead of every time after the pass it around statement?


#2

It is because the function [color=#FF0000]singTheSong ()[/color] is (recursively) calling itself.

printf(“Take one down, pass it around, %d bottles of beer on the wall.\n”, oneFew);
[color=#FF0000]singTheSong (oneFew);[/color]
printf(“Put a bottle in the recylcing, %d empty bottles in the bin. \n”, numberOfBottles);

Here is something similar:

//  main.m

#import <Foundation/Foundation.h>

void FooBar (int n)
{
    printf ("%s: n=%d\n", __PRETTY_FUNCTION__, n);
    if (n > 0)
    {
        FooBar (n-1);  // Note the recursive call
    }
    printf ("%s: n=%d\n", __PRETTY_FUNCTION__, n);
}

int main (int argc, const char * argv[])
{
    FooBar (8);
    return 0;
}

/*
Console output:
void FooBar(int): n=8
void FooBar(int): n=7
void FooBar(int): n=6
void FooBar(int): n=5
void FooBar(int): n=4
void FooBar(int): n=3
void FooBar(int): n=2
void FooBar(int): n=1
void FooBar(int): n=0
void FooBar(int): n=0
void FooBar(int): n=1
void FooBar(int): n=2
void FooBar(int): n=3
void FooBar(int): n=4
void FooBar(int): n=5
void FooBar(int): n=6
void FooBar(int): n=7
void FooBar(int): n=8
*/

#3

right ok. thanks for the reply. i understand that its recursive, however, in my head, i still don’t grasp exactly what that does other than call itself. by calling itself, does it store the values somewhere else to print after the counter gets to 0? i just don’t understand its function and how it prints after the fact.


#4

Without going into exact details, here is what happens when a function is called. When a function is invoked, a certain amount of information is saved including information that allows the function to return to the place immediately following the place of invocation, and information that is required to restore the state of the execution stack when the function returns. The information is saved on a stack; this makes it possible to call a function recursively from within the function itself. I think this is described in the book, so I am not going to go any further.


#5

awesome. that explanation actually helps out a lot. I’m still not 100% on the whole concept, but it actually clicked a few things in my head that make a lot more sense to the process now. thanks again for the replies.