1. 区别:
多个if连用,每一个if都会判断;
if else if…else if…,其中有一个匹配上后面的不会再执行。
2. 举例:
多个if连用:
#include <stdio.h>
int main() {
int num = 0, temp = 6;
if (temp == 3)
num = 1;
if (temp == 6)
num = 2;
if (temp % 2 == 0)
num = 3;
printf("%d\n", num);
return 0;
}
第二第三个都符合条件,也都执行。

if else if:
#include <stdio.h>
int main() {
int num = 0, temp = 6;
if (temp == 3)
num = 1;
else if (temp == 6)
num = 2;
else if (temp % 2 == 0)
num = 3;
printf("%d\n", num);
return 0;
}
第二第三个都符合条件,测试完第二个符合后不再执行后面的else if…。

网友评论