为什么在if .... else语句的情况下使用c语言中的switch? [英] Why we use switch in c language in case of if....else statement?
本文介绍了为什么在if .... else语句的情况下使用c语言中的switch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为使用if ... else语句,我们只能比较一个条件.
Switch是一个多选语句.
Because using if...else statement we can compare only one condition.
Switch is a multiple selection statement.
推荐答案
switch语句速度更快,因为编译器可以优化switch
语句,而不能优化if/else
阶梯.您使用的更多的是样式问题,而不是其他任何问题.
有时,您根本无法使用switch语句,而这些实例几乎是我唯一不使用switch
语句的时间.
The switch statement is faster because the compiler can optimize theswitch
statement, whereas it cannot optimize aif/else
ladder. Which you use is more a matter of style than anything else.
Sometimes, you simply can''t use a switch statement, and those instances are pretty much the only time I won''t use aswitch
statement.
您从未听说过嵌套 if ... then ... else ?
Have you never heard of nested if...then...else?
But hey thanks for sharing.
switch
语句是if
-else
链的简写形式,只要测试是在返回' 整数类型"(请参见 C ++开关语句 [ ^ ]) .
那就是if-else
链更通用,更强大,而switch
语句通常很方便.
:)
Theswitch
statement is a short-hand form of anif
-else
chain, provided the test is performed on an expression returning an ''integral type'' ( see The C++ switch statement[^] atMSDN
).
That is theif-else
chain is more general and powerful, while theswitch
statement is often handy.
:)
这篇关于为什么在if .... else语句的情况下使用c语言中的switch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文