简体   繁体   English

C ++ switch语句的用法

[英]C++ Usage of switch statement

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? 问题1 >当执行switch语句时,我们是直接跳转到正确的case语句还是从上到下搜索?

Answer : Directly jump to the right case statement. :直接跳转到正确的case语句。

Question 2 > Should we use a break statement after the default statement? 问题2 >我们应该在默认语句之后使用break语句吗?

Answer : Depends. :视情况而定。 If the default statement is the last case statement, then using break is NOT necessary. 如果默认语句是最后一个case语句,则不需要使用break。

Did I get the answers right in the above questions? 在上述问题中我是否得到正确的答案?

Question 1: Depends on the compiler. 问题1:取决于编译器。 C++ standard does not require that a jump table be set up. C ++标准不需要设置跳转表。

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). 在许多情况下,尤其是在少数情况下,GCC,MSVC和其他编译器将按子句进行检查(就像是if语句一样)。 To give an example, suppose your cases were 1, 15, and 1000000. It would not be efficient code-wise to do a direct jump. 举个例子,假设您的情况是1、15和1000000。直接跳转在代码方面效率不高。

gcc has the option -fno-jump-tables to force it to build the equivalent if-else list. gcc具有选项-fno-jump-tables来强制它构建等效的if-else列表。

Question 2: The break statement is not required for the last clause. 问题2:最后一个子句不需要break语句。 It should be omitted if execution should flow down. 如果执行顺畅,则应将其省略。

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. 您对答案2是正确的,除了我会补充说,如果默认语句是最后一种情况, 或者不是,并且您不希望其失败,则必须在默认语句后进行休息。 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). 如果优化较低或被禁用(或者您的编译器很旧和/或不是很复杂),它将从上到下进行搜索,跳过默认情况(感谢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. 如果启用了优化,并且编译器决定执行优化,则会将其优化到跳转表中,在这种情况下,它将直接跳转到正确的情况。

Question 1> When the switch statement executes, do we jump directly to the right case statement or do we search from top to bottom? 问题1>执行switch语句时,我们是直接跳转到正确的case语句还是从上到下搜索?

I don't think there is any mention about this implementation detail in the standard. 我认为标准中没有提及此实现细节。 However, your answer is correct for that. 但是,您的答案是正确的。

Question 2> Should we use a break statement after the default statement? 问题2>我们应该在默认语句之后使用break语句吗?

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 say case 1: . 考虑一下这种情况,您想在default:情况下执行一些操作default: case,而要说出case 1:事情要少一些。 For example, 例如,

switch(i)
{
  default: foo();
  case 1:  bar();
           break;
  case 2:  abc();
           break;
  case 3:  xyz();
           break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM