Hey guys,
So, I have started re-reading a book for iOS programming. I already read a lot of this book - but never took the time to 100% understand every bit of code / challenge - this was a mistake. So I stopped and have restarted.
Book: Objective-C Programming: The Big Nerd Ranch Guide
Author: Aaron Hillegass
Page: 98
Chapter: 5.
It's the Beer Song code.
What I would like to know is:
I know that inside of main.c - I am called the function signTheSong. I understand how that part works - what I want to know is why do I call the fuction again, within the function and how does this code appear to loop ( It counts from 99 to 0) when there is no for / while loop code present?
Lastly - how does the code for "Putting it in the bin, % bottles in the bin" work?
I guess what I am trying to understand is the flow of the code - I understand it as this:
main kicks off "signTheSong.
signTheSong function carries out the If / else statement - does this if / else continue to execute as long as the value is true? (There are still bottles left and "numberOfBottles" is not equal to zero )
I would appreciate any help in understanding this code.
So, I have started re-reading a book for iOS programming. I already read a lot of this book - but never took the time to 100% understand every bit of code / challenge - this was a mistake. So I stopped and have restarted.
Book: Objective-C Programming: The Big Nerd Ranch Guide
Author: Aaron Hillegass
Page: 98
Chapter: 5.
It's the Beer Song code.
What I would like to know is:
I know that inside of main.c - I am called the function signTheSong. I understand how that part works - what I want to know is why do I call the fuction again, within the function and how does this code appear to loop ( It counts from 99 to 0) when there is no for / while loop code present?
Lastly - how does the code for "Putting it in the bin, % bottles in the bin" work?
Code:
#include <stdio.h>
void singTheSong (int numberOfBottles)
{
if (numberOfBottles == 0){
printf ( "There are sinply no more bottles of beer on the wall\n");
}else {
printf ( "%d bottles of beer on the wall, %d bottles of beer\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall\n", oneFewer);
singTheSong(oneFewer);
printf ( "Put a bottle in the bin, %d bottles in the bin\n", numberOfBottles);
}
}
int main(int argc, const char * argv[])
{
singTheSong(99); //This calles the singTheSong function
return 0;
}
I guess what I am trying to understand is the flow of the code - I understand it as this:
main kicks off "signTheSong.
signTheSong function carries out the If / else statement - does this if / else continue to execute as long as the value is true? (There are still bottles left and "numberOfBottles" is not equal to zero )
I would appreciate any help in understanding this code.