if語句每次判斷只能實現兩條分支,如果要實現多種選擇的功能,那麼可以采用switch語句。switch語句根據一個控制表達式的值選擇一個內嵌語句分支來執行。它的一般格式為:
switch(controllong-expression)
{
case constant-expression
embedded-statements
default:
embedded-statements
}
switch語句的控制類型,即其中控制表達式(controllong-expression)的數據類型可以是sbyte,byte,short,ushort,uint,long,ulong,char,string或枚舉類型(enum-type)。每個case標簽中的常量表達式(constant-expression)必須屬於或能隱式轉換成控制類型。如果有兩個或兩個以上的case標簽中的常量表達式相同,編譯時將會報錯。switch語句中最多只能有一個default標簽。
我們舉一個例子來說明switch語句是如何實現程序的多路分支的。
假設考查課的成績按優秀、良好、中等、及格和不及格分為五等,分別用4、3、2、1、0來表示,但實際的考卷為百分制,分別對應的分數為90-100,80-90,60-80,60分以下。下面的程序將考卷成績x轉換為考查課成績y。我們先看流程圖。
代碼如下:
int x=int(x/10);
switch(x)
{
case 10:y=4;break;
case 9:y=4;break;
case 8:y=3;break;
case 7:y=2;break;
case 6:y=1;break;
default:y=0;
}
下面的例子判斷傳遞給應用程序的參數的有無、位數。
程序清單8-2:
using System;
class Test
{
public static void Main(string[] args{
switch(args.Length){
case 0:
Console.WriteLine("No arguments were provided");
break;
case 1:
Console.WriteLine("One arguments was provided");
break;
default:
Console.WriteLine("{0}arguments were provided");
break;
}
}
使用switch語句時需注意以下幾點:
不准遍歷
C和C++語言允許switch語句中case標簽後不出現break語句,但C#不允許這樣,它要求每個標簽項後使用break語句或跳轉語句goto,即不允許從一個case自動遍歷到其它case,否則編譯時將報錯。
一個程序用於計算一年中已度過的天數,month表示月份,day表示日期,計算結果保存在total中。為簡便起見,我們把閏年的情況排除在外。C和C++程序員會利用一點技巧來實現這個程序:
total=365;
switch(month){
case 1:total-=31;
case 2:total-=28;
case 3:total-=31;
case 4:total-=30;
case 5:total-=31;
case 6:total-=30;
case 7:total-=31;
case 8:total-=31;
case 9:total-=30;
case 10:total-=31;
case 11:total-=30;
case 12:total-=31;
default:total+=day;
}
然而這種寫法在C#中是不允許的。有經驗的程序員會利用這個特點,但是很難保證任何人在編程時都不會忘記在case後面加一break語句,這時往往會造成一些不易察覺的錯誤。所以在C#中,如果標簽項沒有出現break語句或跳轉語句goto,編譯器將會要求程序員加上。
如果想象c,c++那樣,執行完後繼續遍歷其它的語句,那也不難,只需要明確地加入這兩條跳轉語句即可:
●goto case label:跳至標簽語句執行
●goto default:跳至default標簽執行
那樣,我們將上面的程序可以改寫為:
total 365;
switch(month){
case 1:total-=31;goto case 2;
case 2:total-=28;goto case 3;
case 3:total-=30;goto case 4;
case 4:total-=31;goto case 5;
case 5:total-=30;goto case 6;
case 6:total-=31;goto case 7;
case 7:total-=30;goto case 8;
case 8:total-=31;goto case 9;
case 9:total-=30;goto case 10;
case 10:total-=31;goto case 11;
case 11:total-=30;goto case 12;
case 12:total-=31;goto default;
default:total+=day;
}
在避免了c,c++中常出現的由於漏寫break而造成的錯誤的同時,“不准遍歷”的原則還使得我們可以任意排列switch語句中的case項而不會影響switch語句的功能。
另外,一般來說每個switch項都以break,goto case或goto default結束,但實際上任何一種不導致“遍歷”的結構都是允許的,例如throw和return語句同樣可以跳出控制之外,因而下例是正確的:
switch(i){
case 0:
while(true) F();
case 1:
throw new ArgumentException();
case 2:
return;
}
把字符串當成常量表達式
VB程序員可能已經習慣把字符串當成常量表達式來使用,但C和C++卻不支持這一點。但是,C#的switch語句與c,c++的另一個不同點是,C#可以把字符串當成常量表達式來使用。所以switch語句的控制類型可以是string類型。
下面的例子是實現浮動窗口提示。在Windows操作系統中,我們把鼠標移到某一個控件上停留幾秒鐘,將會出現一個浮動提示,說明該控件的作用等。例子中的GetButtonCaption方法用於獲得按鈕上的文字,ShowMessage表示在浮動提示窗口中顯示信息。
string text=GetButtonCaption();
switch(text)
{
case "OK":ShowMessage("save the change and exit");break;
case "Retry":ShowMessage("return and retry");break;
case "Abort":ShowMessage("Abort the change and exit");break;
case "Help":ShowMessage("Get help from system");break;
}
實際上,在老版本的C語言中,switch語句的控制類型只允許是整數表達式或字符型表達式,而ANSI標准放寬了這一要求。C#中是對switch語句的控制類型的又一擴展。而且在C#中case標簽的引用值允許是null。