在C语言中使用switch()语句的效率是否比几个if()语句高? switch()可以编译为多个if()语句吗?例如:
int a = 5;
switch(a) {
case 5:
printf("Number 5\n");
break;
case 3:
printf("Number 3\n");
break;
default:
printf("Other\n");
break;
}
与之相对:
int a = 5;
if (a == 5) {
printf("Number 5\n");
}
if (a == 3) {
printf("Number 3\n");
}
if (a != 5 && a != 3) {
printf("Other\n");
}