Basic usage and break of switch

Source: Internet
Author: User

First, the basic usage of switch

The switch statement is a multi-branch selection statement used to implement a multi-branch selection structure. Its general form is as follows:

switch (expression)

{

case constant Expression 1: statement 1

Case constant Expression 2: statement 2

......

case constant Expression N: statement n

Default: Statement n+1

}

which

1) "expression" in parentheses after switch, the result can be an integer value, or it can be a character type of data.

2) When the value of the switch expression is the same as the value of a constant expression in a case statement, the statement in this case is executed, and if none of the values of the switch expression match, then the statement in the default is executed.

3) Each case expression must produce a unique value.

Ii. switch vs. break

Special: Caseonly determines the entrance of the program execution, using break can be reasonable chunking . That is, after the program executes a case clause, the next case clause is executed sequentially. Break allows you to jump out of the switch selection structure after executing a necessary case clause, which solves some problems that cannot be achieved at the same time.

For example:

  • #include <stdio.h>
    int main ()
    {
    int n=7;
    Switch (n)
    {
    Case 1:
    printf ("1\n");
    Case 7:
    printf ("7\n");
    Case 8:
    printf ("8\n");
    Default
    printf ("default\n");
    Break
    }
    return 0;
    }

The results are as follows:

7

8

Default

If each case clause has a break statement, the result will be:7

Note:

1) In general, add a break statement in the last case clause.

2) You also need to add a break statement after default.

3) If there are more than one statement in the case clause, the {} is applied.

4) Multiple case can share one execution statement.

This article is from the "unintentional persistent" blog, please be sure to keep this source http://10740590.blog.51cto.com/10730590/1700639

Basic usage and break of switch

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.