switch
#include <stdio.h> int main(int argc, char * argv[]) { int i; scanf("%d",&i); switch(i) { case 1: {printf("春"); break; } case 2: { printf("夏"); break; } case 3: { printf("秋"); break; } case 4: { printf("冬"); } default: break; } }
多个case可以运行一个语句块
case中定义变量需要加{};
枚举类型(enum)
1
提高程序的可读性,本质是整形,用枚举类型代表整数常量;
enum Season {
spring,
summer,
autumn,
winter
}
//默认从零开始
enum Season aeason=0;
scanf("%d,&season");
switch(season)
{
case spring:
{
printf("spring");
}
case summer:
{
printf("spring");
}
case autumn:
{
printf("autumn");
]
}
#include <stdio.h> enum Season { spring=1, summer, autumn, winter }; typedef enum Season myseason ;//为现有类型创建一个别名 int main(int argc, char * argv[]) { //默认从零开始 myseason season=0; scanf("%d",&season); switch(season) { case spring: { printf("spring"); break; } case summer: { printf("summer"); break; } case autumn: { printf("autumn"); break; } case winter: { printf("winter"); break; } default: { printf("未知"); } } }