Analyzing the specific function of break statement in C language switch _c language

Source: Internet
Author: User
Tags case statement

Problem:
Break plays a role in the loop flow control of the for loop, while loop, and stops execution of the statement after the break, jumps out of the loop, and jumps out of the loop control body;
In the switch condition selection, there is no circular control, what is the role of the break?

Solution:
1. The execution flow of the switch statement is:
The value of the expression in the parentheses after the switch is evaluated first, and then the constant expression of each case is compared with this value.
If the value of the expression in the parentheses is equal to the value of the constant expression after the case, execute the statement following the case, execute the break statement and exit the switch statement, and the program flow turns to the next statement of the switch statement;
If the value of an expression in parentheses is unequal to the constant expression after all the case, execute the statement following the default, and then exit the switch statement, and the program flow turns to the next statement of the switch statement.
In a switch-case statement, multiple case can share a single execution statement, such as:
case constant Expression 1:
Case constant Expression 2:
Case constant Expression 3:
Statement
Break
The effect of case statements can be seen:
The constant expression at the end of the case actually only acts as a statement label, but does not have a condition to judge the function, that is, "just start the entrance marking of the execution department".

Therefore, once the value of the expression in the parentheses behind the switch matches, the execution begins at this label;
And after executing a statement after a case, if you do not encounter a break statement, you automatically go to the next cases to continue execution, and no longer determine whether to match, until the break statement to stop execution, exit the switch statement.

Therefore, if you want to jump out of the switch statement immediately after executing a single case, you must add a break statement at the end of this branch.

2. As you can see from the above description, the role of the break in the switch conditional selection statement is essentially the same as in the circular control statement:
Are statements that do not execute after a break, and exit the switch statement;

3. Unlike the continue statements in the loop control, there is no continue statement in the switch conditional selection statement;

Ok problem solved.

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.