作者:开心小笨笨猪 | 来源:互联网 | 2023-09-15 17:03
最近在学习objective-c,教程中有一段代码没弄清楚,是关于递归的,我用截图的形式发上来,给大家看看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| void singTheSong(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("there are simply 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 dowm, pass it around, %d bottles of beer on the wall.\n",
oneFewer);
singTheSong(oneFewer);
printf("put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
}
}
int main(int argc, const char * argv[])
{
singTheSong(99);
return 0;
} |
它的输出结果是这样的:
99 bottles of beer on the wall. 99 bottles of beer.
Take one dowm, pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall. 98 bottles of beer.
Take one dowm, pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall. 97 bottles of beer.
Take one dowm, pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall. 96 bottles of beer.
Take one dowm, pass it around, 95 bottles of beer on the wall.
......(中间重复的省略)
1 bottles of beer on the wall. 1 bottles of beer.
Take one dowm, pass it around, 0 bottles of beer on the wall.
there are simply no more bottles of beer on the wall.
put a bottle in the recycling, 1 empty bottles in the bin.
put a bottle in the recycling, 2 empty bottles in the bin.
......(中间重复的省略)
put a bottle in the recycling, 98 empty bottles in the bin.
put a bottle in the recycling, 99 empty bottles in the bin.
Program ended with exit code: 0
这段代码,我看不懂的地方是,它如何从numberOfBottles等于0的时候,又继续运行了
1 2
| printf("put a bottle in the recyling, %d empty bottles in the bin.\n",
numberOfBottles); |
这段代码呢?并且numberOfBottles一直在+1
请大家帮我解惑,谢谢了~