为什么开关/箱,而不是的if / else如果? [英] Why Switch/Case and not If/Else If?

查看:217
本文介绍了为什么开关/箱,而不是的if / else如果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主要这个问题指着C / C ++,但我想其他语言相关的为好。

This question in mainly pointed at C/C++, but I guess other languages are relevant as well.

我不明白为什么交换机/箱仍在使用,而不是的if / else如果。在我看来,就像使用goto语句的,并导致相同的排序杂乱code,而同样的结果可能与被acheived的if / else,如果在一个更加有组织的方式。

I can't understand why is switch/case still being used instead of if/else if. It seems to me much like using goto's, and results in the same sort of messy code, while the same results could be acheived with if/else if's in a much more organized manner.

不过,我看到周围往往这些块。找到他们的共同的地方是靠近一个消息循环(的WndProc ...),而这些都是地方之中时,他们提出的最重的破坏:变量沿整个块共享,即使当不propriate(并且不能里面初始化)。格外重视对不会放弃休息的被投入,等等...

Still, I see these blocks around quite often. A common place to find them is near a message-loop (WndProc...), whereas these are among the places when they raise the heaviest havoc: variables are shared along the entire block, even when not propriate (and can't be initialized inside it). Extra attention has to be put on not dropping break's, and so on...

就个人而言,我避免使用它们,不知阉我失去了一些东西?

Personally, I avoid using them, and I wonder wether I'm missing something?

是不是如果/其他的他们更有效率?
难道他们按照传统进行的?

Are they more efficient than if/else's? Are they carried on by tradition?

推荐答案

Summarising我最初的帖子和评论 - 有超过开关语句的几个优点如果 / 其他语句:

Summarising my initial post and comments - there are several advantages of switch statement over if/else statement:


  1. 清洁code。 code有多个链接如果 / 的if else ... 看起来混乱和难以维持 - 开关给吸尘器结构。

  1. Cleaner code. Code with multiple chained if/else if ... looks messy and is difficult to maintain - switch gives cleaner structure.

性能。对于密集情况值编译器生成跳表,稀疏 - 如果 / 的二进制搜索或一系列>其他,所以在最坏的情况下开关是尽可能快地如果 / 其他,但通常速度更快。尽管一些编译器同样可以优化如果 / 其他

Performance. For dense case values compiler generates jump table, for sparse - binary search or series of if/else, so in worst case switch is as fast as if/else, but typically faster. Although some compilers can similarly optimise if/else.

测试顺序并不重要。为了加快如果 / 系列的其他测试一个人需要首先把更可能的情况。随着开关 / 情况程序员不需要考虑这个问题。

Test order doesn't matter. To speed up series of if/else tests one needs to put more likely cases first. With switch/case programmer doesn't need to think about this.

默认可以在任何地方。随着如果 / 其他默认情况下,必须在最后 - 最后其他。在开关 - 默认可以在任何地方,无论程序员发现它比较合适。

Default can be anywhere. With if/else default case must be at the very end - after last else. In switch - default can be anywhere, wherever programmer finds it more appropriate.

通用code。如果您需要执行共同code几种情况下,您可以省略,执行会落空 - 这是你不能用如果 / 其他。 (这是一个很好的做法放置一个特殊的注释 / * * FALLTHROUGH /这类案件 - 皮棉识别并没有抱怨,没有对此有何评论它不抱怨,因为它是常见的错误忘记)。

Common code. If you need to execute common code for several cases, you may omit break and the execution will "fall through" - something you cannot achieve with if/else. (There is a good practice to place a special comment /* FALLTHROUGH */ for such cases - lint recognises it and doesn't complain, without this comment it does complain as it is common error to forgot break).

感谢所有评论者。

这篇关于为什么开关/箱,而不是的if / else如果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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