C Language BASIC programming Knowledge: Switch switch statement analysis

Source: Internet
Author: User
Tags case statement constant

In the C language choice structure, the If--else statement generally refers to the choice structure which handles one or two branches, if the branch more also uses the If_else statement to be able to cause the program to run the efficiency to reduce, the code readability is reduced. So the switch came into being. Switch's English meaning is "toggle", so it is in programming to make a choice branch of professional terminology.

First, we'll communicate the basic syntax of the switch statement.

switch (expression)//evaluates the value of an expression
{
CASE constant 1://If equal to constant 1
Statement You can only add constants after the case
Break Stepping out of the branch, executing the code after the switch statement

Case constant 2://If equal to constant 2
Statement
Break

Default://If no matching value is found
Statement
Break
}
The execution process of a switch statement

The order of execution in the switch statement is Top-down, and it first evaluates the value of the expression, and executes the corresponding CASE statement if it finds that the value of the expression is equal to the constant value behind it. If no constant is found that matches the expression value, the program executes the default section of code.

PS: When you use the switch, you usually use the break statement. (break statements are used to jump out of this selection or loop, to execute a selection or to loop through the code) because since we choose to use the switch statement, then the branch must be more, what is a branch? We will not discuss its theoretical significance for the moment, we just need to know that the conditions of the branch must not be equal (if it is, then the separation of what to do, all the and for one do not forget?) Since there is no equality, then you must let the program another "tends" thought, so we can add a break after a case, go home qinggong.

Next we understand some of the experiences of switch in use

1, Swtich statements to be used in conjunction with Case,break.
2, the conditional judgment of the expression type can be any type, commonly used is an integral expression, character expression, enumeration type.
3, after the case can only add constants, that is, only integer constants or string constants (C language does not have a string, so to achieve the effect of the string is generally using a character array) The most important thing is that the constant value after each case must be different from each other and not interfere with each other. Otherwise, there will be a logic error in the program.
4, Case and case,case and default order can be disrupted, the disruption will not affect the implementation of the effect.

It technology network: http://www.52ij.com/Small modest, if reproduced, please keep the original address!

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.