The switch statement cannot be traversed"

Source: Internet
Author: User

C and C ++ allow the case label in the switch statement without the break statement, so that you can continue to execute the following statement after case. However, this is often because programmers forget to write break statements and become an undiscoverable vulnerability. Therefore, C # does not allow this. It requires that each label item use the break statement or jump statement goto, that is, it is not allowed to automatically traverse from one case to other cases. Otherwise, an error is reported during compilation, even after the default statement.


This determines three features:

A) We can arrange case items in any switch statement without affecting the switch statement function.

B) If you want to traverse other statements after execution like c/c ++, add one of the following two jump statements after the case statement: goto case label; goto default.

3) Besides goto, throw and return can also be used.


Another difference between the C # switch statement and C/C ++ is that C # uses string constants as constant expressions like VB, but C/C ++ does not. So in C #, case "OK": This form is also supported.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.