C語言中的select語句是一種條件判斷語句,用于根據不同的條件執行不同的操作。它的基本使用方法如下:
1.基本語法
select(expression){
case constant-expression:
statement(s);
break;
case constant-expression:
statement(s);
break;
default:
statement(s);
}
2.使用case語句
int a = 2;
switch(a){
case 1:
printf("a=1");
break;
case 2:
printf("a=2");
break;
default:
printf("a is not 1 or 2");
}
3.使用default語句
int a = 3;
switch(a){
case 1:
printf("a=1");
break;
case 2:
printf("a=2");
break;
default:
printf("a is not 1 or 2");
}
4.使用多個case語句
int a = 3;
switch(a){
case 1:
case 2:
printf("a is 1 or 2");
break;
default:
printf("a is not 1 or 2");
}
5.使用break語句
int a = 2;
switch(a){
case 1:
printf("a=1");
break;
case 2:
printf("a=2");
break;
default:
printf("a is not 1 or 2");
}
6.使用嵌套switch語句
int a = 1;
int b = 2;
switch(a){
case 1:
switch(b){
case 1:
printf("a=1, b=1");
break;
case 2:
printf("a=1, b=2");
break;
}
break;
case 2:
printf("a=2");
break;
default:
printf("a is not 1 or 2");
}