我正在使用 nig Turbo C++ 编译器学习 C 语言,正好遇到以下两个语句:
- IF(嵌套许多 IF)
- IF-else(不是嵌套而是继续else,else等等)
我想知道我的想法是否正确,IF(嵌套有许多 IF)和 IF-else(不嵌套)是否相同?非常感谢您的建议。
最佳答案
这只是背后的基本逻辑:
嵌套的 if
条件:
如果第一个条件的值为真,则进入第二个条件。
if(a > 0)
{
printf("A is greater than 0\n");
if(a > 2) printf("A is greater than 0 and 2\n");
}
if-else
条件:
如果第一个条件的值为假,转到下一个:
if(a > 0) printf("A is greater than zero\n");
else if(a < 0) printf("A is lesser than zero\n");
else printf("A is zero\n");
还有一个你应该知道的说明,switch
:
switch(a)
{
case 0: printf("A is zero\n"); break;
case 1: printf("A is one\n"); break;
case 5: printf("A is five\n"); break;
default: printf("A is not 0, 1 or 5\n"); break;
}
关于c++ - 嵌套 IF 语句与 IF-ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32466045/