Usage of switch syntax in C

Source: Internet
Author: User

Switch is used in programming. For example, it is often used together with case in C language.Code. Its function is to control the flow of business processes.
The syntax of the switch statement is as follows (switch, case, and default are keywords ):
Switch (controllingexpression)
{
Case constantexpression1:
Case constantexpression2:
Case constantexpression2:
Statements; // when constantexpression1, constantexpression2, and constantexpression3 are met, statements is executed.
Break;
Case constantexpression:
Statements;
Break;
...
Default:
Statements;
Break;
}
Observe switch statement rules
The switch statement is very useful, but you must be cautious when using it. Any switch statement must follow the following rules:
Only switch can be used for basic data types, including int and char. For other types, the IF statement must be used.
The case tag must be a constant expression (constantexpression), such as 42 or "42 ". If you need to calculate the value of the Case tag at runtime, you must use the if statement.
The case tag must be a unique expression. That is to say, two cases cannot have the same value.

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.