The usage of C language switch statement _c language

Source: Internet
Author: User

The C language does not limit the number of branches that if else can handle, but when there are too many branches, it is not convenient to use if else, and it is easy to have an if else pairing error. For example, enter an integer that outputs the corresponding number of days of the week in English for that integer:

#include <stdio.h>
int main () {
  int A;
  printf ("Input integer Number:");
  scanf ("%d", &a);
  if (a==1) {
    printf ("monday\n");
  } else if (a==2) {
    printf ("tuesday\n");
  } else if (a==3) {
    printf ("wednesday\n");
  } else if (a==4) {
    printf ("thursday\n");
  } else if (a==5) {
    printf ("friday\n");
  } else if (a==6) {
    printf ("saturday\n");
  } else if (a==7) {
    printf ("sunday\n");
  } else{
    printf ("error\n");
  }
  return 0;
}

Run Result:

Input integer Number:3
Wednesday

The C language also provides an alternative statement--switch statement with multiple branches, and its basic syntax format 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;
}

It is executed by first evaluating the value of the expression. Then, starting with the first case, compared to the constant expression x, if the value of the current constant expression is not equal, then the statement x after the colon is not executed, and once the value of a constant expression is found to be equal, it executes all subsequent statements. If no equal value is found until the last constant expression N, the statement n+1 after default is executed.

It should be noted that when an equal case branch is found, it executes the branch and all subsequent branches of the statement. For example:

#include <stdio.h>
int main () {
  int A;
  printf ("Input integer Number:");
  scanf ("%d", &a);
  Switch (a) {case
    1:printf ("monday\n");
    Case 2:printf ("tuesday\n");
    Case 3:printf ("wednesday\n");
    Case 4:printf ("thursday\n");
    Case 5:printf ("friday\n");
    Case 6:printf ("saturday\n");
    Case 7:printf ("sunday\n");
    default:printf ("error\n");
  }
  return 0;
}

Run Result:

Input integer Number:4
Thursday
Friday
Saturday
Sunday
Error

Enter 4 and the fourth branch is found to match, so the fourth branch and all subsequent branches are executed. This is obviously not the result we want, we want to do only the fourth branch and skip all the branches that follow.

To avoid this, the C language also provides a keyword break that is designed to jump out of a switch statement.

The switch's branch statements are n+1, and we usually want to select one of the branches to execute, end the entire switch statement after execution, and continue with the statements that follow the switch, which can be implemented by adding a break statement after each branch. As follows:

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
}

After adding this break statement, once the value of the constant expression x and the expression is equal, execute statement x, and after the execution is done, jump out of the switch statement and proceed to the program that follows the switch statement, so that unnecessary statements can be avoided.

Use the switch statement to modify the above code:

#include <stdio.h>
int main () {
  int A;
  printf ("Input integer Number:");
  scanf ("%d", &a);
  Switch (a) {case
    1:printf ("monday\n");
    Case 2:printf ("tuesday\n"); break;
    Case 3:printf ("wednesday\n"); break;
    Case 4:printf ("thursday\n"); break;
    Case 5:printf ("friday\n"); break;
    Case 6:printf ("saturday\n"); break;
    Case 7:printf ("sunday\n"); break;
    default:printf ("error\n"); break;
  return 0;
}

Run Result:

Input integer Number:4
Thursday

It is worth mentioning that, since default is the last branch, no other branches will be executed after the match, so there is no break;

The above is the C language switch statements of data collation, learning C language conditional sentence of the students can refer to.

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.