[Learning notes] [C Language] Select structure-switch, learning notes-switch
1. Usage
Switch (integer expression)
{
Case value 1:
Statement 1;
Break;
Case value 2:
Statement 2;
Break;
......
Case value n:
Statement n;
Break;
Default:
Statement n + 1;
Break;
}
When the value of an integer expression is equal to "value 1", "Statement 1" is executed. The break next to the expression indicates that the entire switch statement is exited, that is, the entire switch statement is jumped to the 16th line of code;
When the value of an integer expression is equal to "value 2", "Statement 2" is executed, and so on. If the value ranges from 1 ~ If no value in value n is equal to the value of an integer expression, the statement n + 1 in default is executed.
Since there is a break after all cases, after executing the statements in any case, the switch statement will be exited directly.
2. Roles of break
The break keyword is used to exit the entire switch statement. In the default format, each case is followed by a break. Therefore, after the statement in the case is executed, the switch statement is exited.
1> if there is no break after a case, it means that after the statements in this case are executed, all the statements in the case and default are executed in sequence until a break is encountered.
3. Code
1 # include <stdio. h> 2 3/* 4 if (condition) 5 {6 7} 8 9 10 switch (value) 11 {12 case value statement 1; 14 break; 15 16 case value: 2: 17 Statement 2; 18 break; 19 20 default: 21 Statement 3; 22 break; 23} 24 */25 26 int main () 27 {28 // int a = 10; 29 // break: exit the entire switch statement 30 // if there is no break after the case, all the statements in the subsequent cases will be executed, 31/* 32 int B = 10 until break is encountered; 33 34 switch (a) 35 {36 case printf ("A \ n"); 38 B ++; 39 case 5: 40 printf ("B \ n"); 41 B ++; 42 case 0: 43 printf ("C \ n"); 44 B ++; 45 break; 46 default: 47 printf ("D \ n"); 48 break; 49} 50 51 printf ("the value of B is % d \ n", B ); */52 53 char c = '+'; 54 int a = 10; 55 int B = 20; 56 // if you want to define a new variable after case, 57 switch (c) {58 case '+': 59 {60 int sum = a + B; 61 printf ("and % d \ n ", sum); 62 break; 63} 64 65 case '-': 66 {67 int minus = a-B; 68 printf ("difference: % d \ n", minus ); 69 break; 70} 71} 72 73 return 0; 74}
4. Exercise
1/* 2 enter an integer score to represent the score, according to the score output level (A-E) (in two ways) 3 A: 90 ~ 100 score/10 = 9, 10 4 B: 80 ~ 89 score/10 = 8 5 C: 70 ~ 79 score/10 = 7 6 D: 60 ~ 69 score/10 = 6 7 E: 0 ~ 60 8 */9 10 # include <stdio. h> 11 12 int main () 13 {14 int score = 100; 15 16 switch (score/10) {17 case 10: // 10018 case 9: // 90 + 19 printf ("A \ n"); 20 break; 21 case 8: // 80 + 22 printf ("B \ n"); 23 break; 24 case 7: // 70 + 25 printf ("C \ n"); 26 break; 27 case 6: // 60 + 28 printf ("D \ n "); 29 break; 30 default: 31 printf ("E \ n"); 32 break; 33} 34 35 36 37/* 38 if and switch39 1> if statements can complete the functions, switch may not be able to complete 40 int a = 10; 41 if (a> 100) 42 {43 44} 45 46 2> in some cases, if statements and switch statements can be exchanged for 47 48 3> switch functions, if statements can complete 49 */50 51 return 0; 52}