热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关于一个递归代码的问题~

最近在学习objective-c,教程中有一段代码没弄清楚,是关于递归的,我用截图的形式发上来,给大家看看

最近在学习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

请大家帮我解惑,谢谢了~


   



推荐阅读
author-avatar
开心小笨笨猪
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有