Switch在一些計算機語言中是保留字,其作用大多情況下是進行判斷選擇。以C語言來說,switch(開關語句)常和case break default一起使用。
基本介紹
- 中文名:開關語句
- 外文名:switch
- 屬性:計算機語言保留字
- 類別:選擇結構語句
典型結構
switch(controllingExpression){ case constantExpression1:statement1;break; case constantExpression2:statement2;break; case constantExpression3:statement3;break; default:statement;}
語法
#include<stdio.h>int main(){ double score; printf("請輸入分數:\n"); scanf("%lf",&score); switch((int)(score/10)) { case 10: case 9:printf("A(最好)\n");break; case 8:printf("B(優秀)\n");break; case 7:printf("C(良好)\n");break; case 6:printf("D(及格)\n");break; case 5: case 4: case 3: case 2: case 1: case 0:printf("E(不及格)\n");break; default:printf("Error!\n"); }}
#include<stdio.h>int main(){ char rank; printf("請輸入分數等級:(A、B、C、D、E)\n"); scanf("%c",&rank); switch(rank) { case'A':printf("A(90~100)\n");break; case'B':printf("B(80~89)\n");break; case'C':printf("C(70~79)\n");break; case'D':printf("D(60~69)\n");break; case'E':printf("E(0~59)\n");break; default:printf("error!\n");break; }}
#include<stdio.h>int main(){ int year,month,date,leap,days = 0; printf("請輸入年份:"); scanf("%d",&year); printf("請輸入月份:"); scanf("%d",&month); printf("請輸入日:"); scanf("%d",&date); leap=(year%4==0&&year%100!=0||year%400==0); switch(month) { case 12:days+=30; case 11:days+=31; case 10:days+=30; case 9:days+=31; case 8:days+=31; case 7:days+=30; case 6:days+=31; case 5:days+=30; case 4:days+=31; case 3:days+=28+leap; case 2:days+=31; case 1:days+=date;break; default:printf("data error");break; } printf("%4d年%2d月%2d日是這一年的第%d天。\n",year,month,date,days); return 0;}
{
case '<':
target.Text += "<";
break;
case '>':
target.Text += ">";
break;
case '&':
target.Text += "&";
break;
case '\"':
target.Text += """;
break;
case '\'':
target.Text += "'";
break;
default:
target.Text += current;
break;
<scripttype="text/javascript">//Youwillreceiveadifferentgreetingbased//onwhatdayitis.NotethatSunday=0,//Monday=1,Tuesday=2,etc.vard=newDate()theDay=d.getDay()switch(theDay){case5:document.write("FinallyFriday")breakcase6:document.write("SuperSaturday")breakcase0:document.write("SleepySunday")breakdefault:document.write("I'mlookingforwardtothisweekend!")}</script>
<?phpif($i==0){echo"iequals0";}elseif($i==1){echo"iequals1";}elseif($i==2){echo"iequals2";}switch($i){case0:echo"iequals0";break;case1:echo"iequals1";break;case2:echo"iequals2";break;}?>
<?phpswitch($i){case"apple":echo"iisapple";break;case"bar":echo"iisbar";break;case"cake":echo"iiscake";break;}?>
<?phpswitch($i){case0:echo"iequals0";case1:echo"iequals1";case2:echo"iequals2";}?>
<?phpswitch($i){case0:case1:case2:echo"iislessthan3butnotnegative";break;case3:echo"iis3";}?>
<?phpswitch($i){case0:echo"iequals0";break;case1:echo"iequals1";break;case2:echo"iequals2";break;default:echo"iisnotequalto0,1or2";}?>
<?phpswitch($i):case0:echo"iequals0";break;case1:echo"iequals1";break;case2:echo"iequals2";break;default:echo"iisnotequalto0,1or2";endswitch;?>
case expr2:
statements;
break;
case expr3:
statements;
break;
default:
statements;
break;
}
可見,一個switch語句可以代替多個if-else語句組成的分支結構,而switch語句從思路上顯得更清晰。
使用switch語句時,要注意expr1必須是符合byte,char,short,int類型的常量表達式,而不能用浮點類型或long類型,//(也不能為一個字元串)。 (jdk7後,允許String)
2 case 0:
3 setBackground(colo. red);
4 break;
5 case 1:
6 setBackground(color.green) ;
7 break;
8 default:
9 setBackground(color.black);
10 break;
11 }
1 switch(colorNum){
2 case 0:
3 case 1:
4 setBackground(color.green) ;
5 break;
6 default:
7 setBackground(color.black);
8 break;
9 }