| 
                        
                           
                        
                        
                            這種switch-case結構,曾在Algo1語言上使用。 
                            
                            雖然使用 if-else語法可以達到許多條件設定目的, 
                            但是,if-else在閱讀上,對於初學者來說稍嫌吃力, 
                            C 提供另一種結構:switch(開關)。
                             
                            根據變數或運算式的 整數結果,來提供判斷選擇的分支處理, 
                            就好比是旋轉的按鈕。 
                            許多工程師在做條件處理時,可以依照傳回值的分類,有系統的去作設定處置。 
                            因為只有整數能提供精確快速的判斷,也可以使用char型態。
                             
                                - switch 語法格式
                                    
                                    switch ( 運算式 或 
                                    變數) 
                                    { 
                                       case 運算結果1: //case 
                                    和運算結果數值的中間需空一格space. 
                                              程式區A; 
                                              
                                    break; 
                                     
                                       case 運算結果2: 
                                              程式區B; 
                                              
                                    break; 
                                     
                                       case 運算結果3: 
                                              程式區C; 
                                              
                                    break; 
                                            ... 
                                       default: 
                                    //當上述情況都不符, 執行default 
                                              程式區; 
                                              
                                    break; 
                                    } // ← 最後不用再加上";" 
                                     
                                 - 範例一
                                    
                                    #include <stdio.h> 
                                    main() 
                                    { 
                                      
                                    int n; 
                                      
                                    printf("n(1~6)="); 
                                      
                                    scanf("%d",&n); 
                                      
                                    switch(n) 
                                      
                                    { 
                                           
                                    case 1:   printf(" A \n"); 
                                                         
                                    break; 
                                           
                                    case 2:   printf(" B \n"); 
                                                         
                                    break; 
                                           
                                    case 3:   printf(" C \n"); 
                                                         
                                    break; 
                                           
                                    case 4:   printf(" D \n"); 
                                                            break; 
                                           
                                    case 5 ... 10:  printf(" E \n"); 
                                                            break; 
                                           
                                    default:  printf("請勿輸入其他數值\n"); 
                                                         
                                    break; 
                                      
                                    } 
                                    }
                                     
                                    請輸入1~6,將結果寫下;拿掉break;後,再執行1~6,比較前面的結果。 
                                     
                                 - 範例二
 
                                    12 星座 
                                    
                                        
                                            | 項次 | 
                                            日期 | 
                                            星座 | 
                                            英文 | 
                                         
                                        
                                            | 1 | 
                                             1.20 ~ 2.18 | 
                                            水瓶座 | 
                                             Aquarius | 
                                         
                                        
                                            | 2 | 
                                             2.19 ~ 3.20 | 
                                            雙魚座 | 
                                             Pisces | 
                                         
                                        
                                            | 3 | 
                                             3.21 ~ 4.20 | 
                                            牡羊座 | 
                                             Aries | 
                                         
                                        
                                            | 4 | 
                                             4.21 ~ 5.20 | 
                                            金牛座 | 
                                             Taurus | 
                                         
                                        
                                            | 5 | 
                                             5.21 ~ 6.21 | 
                                            雙子座 | 
                                             Gemini | 
                                         
                                        
                                            | 6 | 
                                             6.22 ~ 7.22 | 
                                            巨蟹座 | 
                                             Cancer | 
                                         
                                        
                                            | 7 | 
                                             7.23 ~ 8.22 | 
                                            獅子座 | 
                                             Leo | 
                                         
                                        
                                            | 8 | 
                                             8.23 ~ 9.22 | 
                                            處女座 | 
                                             Virgo | 
                                         
                                        
                                            | 9 | 
                                             9.23 ~ 10.23 | 
                                            天秤座 | 
                                             Libra | 
                                         
                                        
                                            | 10 | 
                                             10.24 ~ 
                                                11.22 | 
                                            天蠍座 | 
                                             Scorpio | 
                                         
                                        
                                            | 11 | 
                                             11.23 ~ 12.21 | 
                                            射手座 | 
                                             Sagittarius | 
                                         
                                        
                                            | 12 | 
                                             12.22 ~ 
                                                1.19 | 
                                            魔羯座 | 
                                             Capricorn | 
                                         
                                     
                                     
                                    13 星座 
                                    
                                        
                                            | 項次 | 
                                            日期 | 
                                            星座 | 
                                            英文 | 
                                         
                                        
                                            | 1 | 
                                            12.18 ~ 
                                                1.18 | 
                                            射手座 | 
                                             Sagittarius | 
                                         
                                        
                                            | 2 | 
                                            1.19 ~ 
                                                2.15 | 
                                            魔羯座 | 
                                             Capricorn | 
                                         
                                        
                                            | 3 | 
                                            2.16 ~ 3.11 | 
                                            水瓶座 | 
                                             Aquarius | 
                                         
                                        
                                            | 4 | 
                                            3.12 ~ 4.18 | 
                                            雙魚座 | 
                                             Pisces | 
                                         
                                        
                                            | 5 | 
                                            4.19 ~ 5.13 | 
                                            牡羊座 | 
                                             Aries | 
                                         
                                        
                                            | 6 | 
                                            5.14 ~ 6.20 | 
                                            金牛座 | 
                                             Taurus | 
                                         
                                        
                                            | 7 | 
                                            6.21 ~ 7.19 | 
                                            雙子座 | 
                                             Gemini | 
                                         
                                        
                                            | 8 | 
                                            7.20 ~ 8.19 | 
                                            巨蟹座 | 
                                             Cancer | 
                                         
                                        
                                            | 9 | 
                                            8.20 ~ 9.15 | 
                                            獅子座 | 
                                             Leo | 
                                         
                                        
                                            | 10 | 
                                            9.16 ~ 
                                                10.30 | 
                                            處女座 | 
                                             Virgo | 
                                         
                                        
                                            | 11 | 
                                            10.31 ~ 
                                                11.22 | 
                                            天秤座 | 
                                             Libra | 
                                         
                                        
                                            | 12 | 
                                            11.23 ~ 11.29 | 
                                            天蠍座 | 
                                             Scorpio | 
                                         
                                        
                                            | 13 | 
                                            11.30 ~ 
                                                12.17 | 
                                            
                                            蛇夫座 | 
                                             Ophiuchus | 
                                         
                                     
                                     
                                    請寫一個程式,只要輸入出生月、日,會輸出對應英文星座(asterism)。 
                                     
                                    
                                 - 比較:
                                    
                                        - 有無break的差異。
                                        
 - default部分可有可無。
                                    
  
                                     
                                 - 技巧用法
                                    
                                        - switch中最多可有256個case
                                        
 - 通常再作switch判斷時,可以讓數個case的值累積,
 
                                            待最後一個case才執行你要作的工作。 
                                      
                                     
                                 - 說明: 累積的case,亦可來判斷是否為數字(Dev C codes)
 
                                    
                                    #include <stdio.h> 
                                    #include <stdlib.h>  
                                    main() 
                                    {  int getch; 
                                       printf("Input char:"); 
                                       scanf("%c",&getch); 
                                       switch(getch) { 
                                          case '1':  
                                    case '2':  case '3':  case '4':  
                                    case '5': 
                                          case '6':  
                                    case '7':  case '8':  case '9':  
                                    case '0': 
                                              
                                    printf("你輸入的數字是 %c\n",getch); 
                                              
                                    break; 
                                          default: 
                                              
                                    printf("你輸入的不是數字!\n"); 
                                              
                                    break; 
                                       }  
                                      system("PAUSE"); 
                                      return 0; 
                                    } 
                                     
                                    
                              
                                                        
                         
                        
                        
                     |