C++ switch语句的用法
int i = 10;
switch( i )
{
case 1:
// do sth1
break;
case 2:
// do sth2
break;
case 3:
// do sth3
break;
default:
// do sth default
break;
}
问题1> switch语句执行时是直接跳到正确的case语句还是从上到下查找?
答案:直接跳转到正确的case语句。
问题2>我们应该在default语句之后使用break语句吗?
答案:视情况而定。如果default语句是最后一个case语句,则不需要使用break。
我对上述问题的答案正确吗?
int i = 10;
switch( i )
{
case 1:
// do sth1
break;
case 2:
// do sth2
break;
case 3:
// do sth3
break;
default:
// do sth default
break;
}
Question 1> When the switch statement executes, do we jump directly to the right case statement or do we search from top to bottom?
Answer: Directly jump to the right case statement.
Question 2> Should we use a break statement after the default statement?
Answer: Depends. If the default statement is the last case statement, then using break is NOT necessary.
Did I get the answers right in the above questions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题1:取决于编译器。 C++标准不要求设置跳转表。
在许多情况下,特别是在少量稀疏情况下,GCC、MSVC 和其他编译器会逐个子句进行检查(就好像它是一个 if 语句一样)。举个例子,假设您的情况是 1、15 和 1000000。直接跳转在代码方面效率不高。
gcc 有选项 -fno-jump-tables 来强制它构建等效的 if-else 列表。
问题2:最后一个子句不需要break 语句。如果执行应向下流动,则应省略它。
Question 1: Depends on the compiler. C++ standard does not require that a jump table be set up.
In many cases, especially with small number of sparse cases, GCC, MSVC and other compilers will do clause-by-clause check (as if it were an if statement). To give an example, suppose your cases were 1, 15, and 1000000. It would not be efficient code-wise to do a direct jump.
gcc has the option
-fno-jump-tables
to force it to build the equivalent if-else list.Question 2: The break statement is not required for the last clause. It should be omitted if execution should flow down.
您的答案二是正确的,但我要补充一点,如果它是最后一种情况或,并且您不希望它失败,则在默认语句之后需要进行中断。但这只是一个挑剔。
问题一的答案取决于是否启用优化以及编译器的智能程度。如果优化较低或被禁用(或者您的编译器很旧和/或不是很复杂),它将从上到下进行搜索,跳过默认情况(感谢 q0987)。如果启用优化并且编译器决定这样做,它将被优化为跳转表,在这种情况下它将直接跳转到正确的情况。
You are correct for answer two, except I would add that a break is necessary after the default statement if it's the last case or its not and you don't want it to fall through. But that's just a nitpick.
The answer to question one depends on if optimisations are enabled and how smart your compiler is. If optimisations are low or disabled (or your compiler is just old and/or not very sophisticated) it will do a search from top to bottom skipping over the default case (thanks q0987). If optimisations are enabled and the compiler decides to do it, it will be optimised into a jump table, in which case it will jump directly to the correct case.
我认为标准中没有提及此实现细节。但是,您的答案是正确的。
是的,这取决于要求。有时您可能根本不需要它。考虑一下这种情况,您想要对
default:
情况做一些事情,而对case 1:
做一些较小的事情。例如,I don't think there is any mention about this implementation detail in the standard. However, your answer is correct for that.
Yes, it depends on the requirement. Sometime you may not need it at all. Consider the situation, where you want to do something for
default:
case and little lesser thing for saycase 1:
. For example,