我对 C 编程语言中的递归有疑问。
我正在读一本关于 c 的书,我得到了这段我不太明白的代码
第一个问题:
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 down, pass it around, %d bottles of beer on the wall.\n",
oneFewer);
singTheSong(oneFewer); // This function calls itself!
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;
}
所以这应该是一首歌。我理解 if 部分
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
我不明白如何从 0 增加到 98
第二个问题:
为什么不一直到99?它停在98。
请用简单的英语解释,因为我不使用英语作为主要语言(有些困难)
谢谢