Home > Article > Backend Development > What does case mean in c language
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.
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;] }
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; }
int a; scanf("%d", &a); switch (a) { case a + 1: break; }
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
Example 1:
#include <stdio.h> int main () { /* local variable definition */ char grade; scanf("%d", &grade); switch(grade) { case 'A' : printf("Excellent!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); break; case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); break; default : printf("Invalid grade\n" ); } printf("Your grade is %d\n", grade ); return 0; }
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; }
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!