[C语言学习笔记]_基础知识_switch case语句
举例:
做一个选择的菜单,可以用if(){} else if(){} else 来写,但是菜单列如果太多了,就显得不是很方便,菜单如下:
printf("****************************************\n");
printf("************* 1.start ***************\n");
printf("************* 2.language ***************\n");
printf("************* 3.exit ***************\n");
printf("****************************************\n");
用switch case语句写如下:
int input_choose;
switch (input_choose)
{
case 1: printf("your choose is 1"); break;
case 2: printf("your choose is 2"); break;
case 3: printf("your choose is 3"); break;
default:printf("input err"); break;
}
用if else 可以写成:
int input_choose;
if (input_choose == 1)
printf("your choose is 1");
else if (input_choose == 2)
printf("your choose is 2");
else if (input_choose == 3)
printf("your choose is 3");
else
printf("input err");
相对来说用switch case写的要好看一点,不那么麻烦.