QC

Mệnh đề switch-case

 Câu lệnh switch trong C là một thay thế cho câu lệnh bậc thang if-else-if cho phép chúng ta thực hiện nhiều thao tác cho các giá trị có thể khác nhau của một biến duy nhất được gọi là biến switch. Ở đây, chúng ta có thể định nghĩa các câu lệnh khác nhau trong nhiều trường hợp cho các giá trị khác nhau của một biến.


Cú pháp của câu lệnh switch trong ngôn ngữ c được đưa ra dưới đây:

  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    

Quy tắc cho câu lệnh switch trong ngôn ngữ C

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as fall through the state of C switch statement.

Hãy cố gắng hiểu nó bằng các ví dụ. Chúng tôi giả định rằng có các biến sau đây.

  1. int x,y,z;  
  2. char a,b;  
  3. float f;  
Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case 'a';case x;
switch(a+b-2)case 1+2;case x+2;
switch(func(x,y))case 'x'>'y';case 1,2,3;


Flowchart of switch statement in C

Hãy xem một ví dụ đơn giản về câu lệnh chuyển đổi ngôn ngữ c.

  1. #include<stdio.h>  
  2. int main(){    
  3. int number=0;     
  4. printf("enter a number:");    
  5. scanf("%d",&number);    
  6. switch(number){    
  7. case 10:    
  8. printf("number is equals to 10");    
  9. break;    
  10. case 50:    
  11. printf("number is equal to 50");    
  12. break;    
  13. case 100:    
  14. printf("number is equal to 100");    
  15. break;    
  16. default:    
  17. printf("number is not equal to 10, 50 or 100");    
  18. }    
  19. return 0;  
  20. }    


Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

Switch case example 2

  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int x = 10, y = 5;   
  5.     switch(x>y && x+y>0)  
  6.     {  
  7.         case 1:   
  8.         printf("hi");  
  9.         break;   
  10.         case 0:   
  11.         printf("bye");  
  12.         break;  
  13.         default:   
  14.         printf(" Hello bye ");  
  15.     }   
  16.           
  17. }  

Output

hi	


Let's try to understand the fall through state of switch statement by the example given below.

  1. #include<stdio.h>  
  2. int main(){    
  3. int number=0;    
  4.   
  5. printf("enter a number:");  
  6. scanf("%d",&number);  
  7.   
  8. switch(number){  
  9. case 10:  
  10. printf("number is equal to 10\n");  
  11. case 50:  
  12. printf("number is equal to 50\n");  
  13. case 100:  
  14. printf("number is equal to 100\n");  
  15. default:  
  16. printf("number is not equal to 10, 50 or 100");  
  17. }  
  18. return 0;  
  19. }    

Output

enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100

Output

enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100


trường hợp chuyển đổi lồng nhau

  1. #include <stdio.h>  
  2. int main () {  
  3.   
  4.    int i = 10;  
  5.    int j = 20;  
  6.    
  7.    switch(i) {  
  8.      
  9.       case 10:   
  10.          printf("the value of i evaluated in outer switch: %d\n",i);  
  11.       case 20:  
  12.          switch(j) {  
  13.             case 20:  
  14.                printf("The value of j evaluated in nested switch: %d\n",j);  
  15.          }  
  16.    }  
  17.      
  18.    printf("Exact value of i is : %d\n", i );  
  19.    printf("Exact value of j is : %d\n", j );  
  20.    
  21.    return 0;  
  22. }  

Output

the value of i evaluated in outer switch: 10
The value of j evaluated in nested switch: 20
Exact value of i is : 10
Exact value of j is : 20 











Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...