哪个更好“切换”或“if-else”或 [英] which is better "switch" or "if-else"

查看:67
本文介绍了哪个更好“切换”或“if-else”或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好用switch语句或if-then等价

的开关?

解决方案

< blockquote>嗨


这取决于条件。如果你只有int或char

常量作为表达式的值,那么switch语句是

简洁易读。


但是如果你有comples值和/或多个范围那么如果

条件更好。


如果你给我们一个你的问题的例子,那么可能有人可以

告诉你哪个是betetr使用。


ju ********** @ yahoo.co.in 写道:

哪个更好用开关声明或if-then等效的开关?




定义更好。

你可以用开关做事

的陈述和漏洞案例很尴尬if /(否则如果/)else阻止。另一方面,

开关仅适用于整数,仅接受

案例标签的常量。


如上所述另一个回复:告诉我们您想要实现的目标

我们可以更详细地评论优惠和反对意见。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。




Michael Mair写道:

ju **********@yahoo.co.in 写道:

使用switch语句或if-then等效的switch更好?



定义更好。
你可以使用switch语句和通过if /(否则如果/)else阻塞的尴尬案例。另一方面,
开关仅适用于整数,仅接受
案例标签的常量。

正如另一个回复中所述:告诉我们你想要实现的目标




我读到某个地方,开关组件生成跳转表

和与if-then等效的相比更快。


Which is better using a switch statement or the if-then equivalent
of switch ?

解决方案

Hi

It depends on the the conditions. If you just have int or char
constants as the value of the expression then switch statement is
concise and easy to read.

However if you have comples values and/or multiple ranges then if
condition is better.

If you give us an example of your problem then possibly someone could
tell you which is betetr to use.


ju**********@yahoo.co.in wrote:

Which is better using a switch statement or the if-then equivalent
of switch ?



Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.

As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.



Michael Mair wrote:

ju**********@yahoo.co.in wrote:

Which is better using a switch statement or the if-then equivalent
of switch ?



Define "better".
You can do things with switch statements and fallthrough cases that
are awkward with if/(else if/)else blocks. On the other hand,
switch works only for integers and accepts only constants for the
case labels.

As remarked in another reply: Tell us what you want to achieve
and we can comment in more detail on the pros and contras.



I read somewhere that assembly of switch generates jump tables
and is faster as compared to if-then equivalent.


这篇关于哪个更好“切换”或“if-else”或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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