为什么在if .... else语句的情况下使用c语言中的switch? [英] Why we use switch in c language in case of if....else statement?

查看:135
本文介绍了为什么在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 the switch statement, whereas it cannot optimize a if/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 a switch statement.


您从未听说过嵌套 if ... then ... else ?

Have you never heard of nested if...then...else?

But hey thanks for sharing.


switch语句是if-else链的简写形式,只要测试是在返回' 整数类型"(请参见 C ++开关语句 [
The switch statement is a short-hand form of an if-else chain, provided the test is performed on an expression returning an ''integral type'' ( see The C++ switch statement[^] at MSDN).
That is the if-else chain is more general and powerful, while the switch statement is often handy.
:)


这篇关于为什么在if .... else语句的情况下使用c语言中的switch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆