Switch-case statement

Source: Internet
Author: User

If statements process two branches, the IF-else-If structure must be used when processing multiple branches. However, if there are many branches, the more nested if statement layers,ProgramIt is not only huge but also difficult to understand. Therefore, the C language provides a conditional selection statement specifically used to process multi-branch structures, calledSwitch statement, Also known as the switch statement. Use the switch statement to directly process multiple branches (including two branches of course). The general form is:
Switch (expression)
{
Case constant expression 1:
Statement 1;
Break;

Case constant expression 2:
Statement 2;
Break;

 

......
Case constant expression N:
Statement N;
Break;

Default:

Statement n + 1;

Break;

}
The execution process of the switch statement is: first calculate the value of the expression in parentheses after the switch, and then use this value to compare it with the constant expressions of each case in sequence, if the value of the expression in parentheses is equal to the value of the constant expression after a case, execute the statement after this case, and then exit the switch statement when the break statement is executed; if the expression value in parentheses is different from the constant expression after all cases, execute the statement n + 1 after default and exit the switch statement. The program flow turns to the next language of the switch statement.

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.