[英]How to use switch statement in c
我如何在以下几行中使用 switch 语句,因为我不想使 if 语句变得庞大。
if(uword1 == (65|69|73|76|78|79|82|83|84|85))
{
total1++;
printf("%i", total1);
}
switch (uword1) {
case 65:
case 69:
case 73:
case 76:
case 78:
case 79:
case 82:
case 83:
case 84:
case 85:
total1++;
printf("%i", total1);
break;
default:
break;
}
是否使用switch
- case
或if
是个人喜好问题。 if
语句不会case
switch
语句更“笨重”:
if( uword1 == 65 ||
uword1 == 69 ||
uword1 == 73 ||
uword1 == 76 ||
uword1 == 78 ||
uword1 == 79 ||
uword1 == 82 ||
uword1 == 83 ||
uword1 == 84 ||
uword1 == 85
)
{
total1++;
printf("%i", total1);
}
对于 switch-case 和 if 语句,代码行数几乎相等。
以下是您在 switch 案例中的操作方式:
switch(uword1)
{
case 65:
case 69:
case 73:
case 76:
case 78:
case 79:
case 82:
case 83:
case 84:
case 85:
total1++;
printf("%i", total1);
break;
default:
break;
}
这是 if 语句中的相同代码:
if(
(uword1==65) ||
(uword1==69) ||
(uword1==73) ||
(uword1==76) ||
(uword1==78) ||
(uword1==79) ||
(uword1==82) ||
(uword1==83) ||
(uword1==84) ||
(uword1==85)
)
{
total1++;
printf("%i", total1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.