Switch case suspect Analysis

Source: Internet
Author: User
Tags switch case

Today, when I read the code, I found a structure similar to the following:

Switch (error)

{

Case ENUM_0:

Case ENUM_1:

Printf ("case 0 and 1 \ n ");

Case ENUM_2:

Printf ("case 2 \ n ");

Break;

}

If the error value is ENUM_0 or ENUM_1, printf ("case 0 and 1 \ n"); this statement must have been executed. But are you sure you want to go to the following case?


In order to verify this small problem, I wrote an example in VC and tried it. It confirmed that it was actually executed in the branch of case 2. It jumps out only when it reaches break.

[Cpp]
# Include <stdio. h>
 
Int main (void)
{
Char ch = 0;
 
Switch (ch)
{
Case 0:
Case 1:
Printf ("case 0 excute \ n ");
Case 2:
Printf ("case 2 excute \ n ");
Break;
}
 
Return 0;
}

To sum up, the statements under the case Branch will jump out when they are executed until break or return.


From fulinwsuafcie's column
 

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.