Home > Backend Development > C#.Net Tutorial > What does case mean in c language

What does case mean in c language

青灯夜游
Release: 2022-11-23 17:35:05
Original
40739 people have browsed it

In C language, case means "situation" and "case", and is the keyword used in switch statements. The "switch case" statement is a selection structure statement that allows testing the equality of variables and value lists. Each value is called a case or case; the program will check the value after the switch and compare it with the value after the case. If If equal, the following code or code block is executed.

What does case mean in c language

The operating environment of this tutorial: windows7 system, c99, Dell G3 computer.

caseWhat does it mean in Chinese

As a noun, it means an example; situation; case; (debate or litigation) argument; box.

As a verb, it means to pack; to check; to check carefully.

case in c language

In c language, case means "situation" and "case". case is the keyword used for switch statements.

The switch case statement is a statement with a selection structure, used to replace a simple if else statement with multiple branches.

The switch statement allows testing the equality of variables and value lists. Each value is called a case or case. The program will check the value after the switch and compare it with the value after the case. If they are equal, the following will be executed. The syntax of code or code block

switch..case in C language is as follows

switch(表达式){
    case 整型数值1: 语句 1;[break;]
    case 整型数值2: 语句 2;[break;]
    ......
    case 整型数值n: 语句 n;[break;]
    default: 语句 n+1;[break;]
}
Copy after login

Its execution process is:

  • 1) First calculate the value of "expression", assuming it is m.

  • 2) Starting from the first case, compare "integer value 1" and m. If they are equal, execute all statements after the colon, that is, start from "statement 1" It is executed until "statement n 1", regardless of whether the subsequent case matches successfully.

  • 3) If "integer value 1" and m are not equal, skip the "statement 1" after the colon and continue to compare the second case and the third case... Once it is found to be equal to an integer value, all subsequent statements will be executed. Assuming that m is equal to "integer value 5", then it will be executed from "statement 5" to "statement n 1".

  • 4) If no equal value is found until the last "integer value n", then the "statement n 1" after default will be executed.

Note:

  • #The expression used in the switch statement must be of int or enum type, otherwise other data types such as float will not be able to It passes the compilation, because the compiler needs the statement after the switch to accurately match the value after the case, and the computer cannot accurately express a float data type

  • switch can be any case statement (including None), use: to separate the value and statement

  • The value following the case must be an int constant value, or the return result is an expression of type int. The following code cannot be compiled and passed

switch (1) {
    case 1.1:
        break;
}
Copy after login
int a;
scanf("%d", &a);
switch (a) {
    case a + 1:
        break;
}
Copy after login
  • When the variable value after the switch matches the constant value after the case, the code after the case will be executed until the break statement is executed and the switch exits. Code block

  • break is not necessary. If there is no break, after the code block of the current case is executed, the content of the subsequent case code block will continue to be executed. It cannot exit until break is executed.

  • switch has a default situation, which we use the default keyword to express. When the variables after switch do not match the constants behind all cases, the statement after default is executed by default

What does case mean in c language

Example 1:

#include <stdio.h>

int main () {

    /* local variable definition */
    char grade;
    scanf("%d", &grade);

    switch(grade) {
        case &#39;A&#39; :
            printf("Excellent!\n" );
            break;
        case &#39;B&#39; :
        case &#39;C&#39; :
            printf("Well done\n" );
            break;
        case &#39;D&#39; :
            printf("You passed\n" );
            break;
        case &#39;F&#39; :
            printf("Better try again\n" );
            break;
        default :
            printf("Invalid grade\n" );
    }

    printf("Your grade is  %d\n", grade );

    return 0;
}
Copy after login

What does case mean in c language

Example 2:

#include <stdio.h>
int main() {

    printf("Please input your grade(1-100):");
    int grade;
    scanf("%d", &grade);

    switch (grade / 10) {
        case 10:
        case 9:
            printf("A\n");
            break;

        case 8:
        case 7:
            printf("B\n");
            break;

        case 6:
        case 5:
            printf("C\n");
            break;
        default:
            break;

    }
    return 0;
}
Copy after login

What does case mean in c language

Related recommendations: "C Video Tutorial"

The above is the detailed content of What does case mean in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template