先计算表达式的值,根据计算值找与之匹配的常量i;若找到,则执行语句组i,遇到break语句后跳出switch结构,否则继续执行下面的语句组;如果没有查到与计算值相匹配的常量,则执行default关键字后的语句
1.
switch(1)
{
case 1:printf("1 ");break;
case 2:printf("2 ");break;
case 3:printf("3 ");
case 4:printf("4 ");
case 5:
case 6:printf("6 ");break;
default:printf("0");
}
输出结果为1
2.
switch(2)
{
case 1:printf("1 ");break;
case 2:printf("2 ");break;
case 3:printf("3 ");
case 4:printf("4 ");
case 5:
case 6:printf("6 ");break;
default:printf("0");
}
输出结果为2
3.
switch(3)
{
case 1:printf("1 ");break;
case 2:printf("2 ");break;
case 3:printf("3 ");
case 4:printf("4 ");
case 5:
case 6:printf("6 ");break;
default:printf("0");
}
输出结果为3 4 6
4.
switch(4)
{
case 1:printf("1 ");break;
case 2:printf("2 ");break;
case 3:printf("3 ");
case 4:printf("4 ");
case 5:
case 6:printf("6 ");break;
default:printf("0");
}
输出结果为4 6
5
switch(4)
{
case 1:printf("1 ");break;
case 2:printf("2 ");break;
case 3:printf("3 ");
case 4:printf("4 ");
case 5:break;
case 6:printf("6 ");break;
default:printf("0");
}
输出结果为4