switch语句解析
switch(开关)语句可以根据一个整数索引值进行多重分支(multi-way branching)。处理多种可能结果的测试时,这种语句特别有用。
程序代码
int switch_prob(int x);void main()
{static int x, y;x=54;y=switch_prob(x);while(1);
}int switch_prob(int x)
{int result=x;switch(x){case 50:case 52:{ result <<= 2;break;}case 53:{ result >>= 2;break;}case 54:result *= 3;case 55:result *= result;default:result += 10;}return result;
}
情况标号值51是缺省值。情况标号值50和52执行语句相同 。情况标号值54和55没有break语句,因而都会执行缺省值的语句。
执行结果
y=0x668e。