我有一段用C语言编写的代码,我想了解程序中的第一个如何解释它后面的if。通常在if-else连词中,如果if中的条件为false,则程序将在else之后执行语句,否则else没有花括号。在下面,下面没有花括号,所以我的问题是:否则会解释“ if(prefer> = 5){printf(“ Buya”); printf(“也许情况很快就会好起来!\ n”);}“作为单个语句,类似于“ else printf(“这是上述else的替换语句”);“建筑?
或者if-else if连词还有另一种逻辑?例如,否则,else仅激活“ if(prefer> = 5)”,并且该条件(如果为true)执行花括号中的内容?
C中的完整代码如下。预先谢谢你。
#include <stdio.h>
int main()
{
int prefer=2;
if (prefer >= 8)
{
printf("Great for you!\n");
printf("Things are going well for you!\n");
}
else if(prefer>=5)
{
printf("Buya");
printf("Maybe things will get even better soon!\n");
}
else if(prefer>=3)
{
printf("Bogus");
printf("Maybe things will get worst soon!\n");
}
else
{
printf("Hang in there--things have to improve, right?\n");
printf("Always darkest before the dawn.\n");
}
return 0;
}
这里是括号括起来的版本,应对此进行解释:
if(prefer >= 8)
{
...
}
else
{
if(prefer >= 5)
{
...
}
else
{
if(prefer >= 3)
{
...
}
else
{
...
}
}
}
else if(condition) ...
没什么特别的。等效于else { if(condition) ... }
。