编程中的决策与位运算操作
1. 简化 switch
语句
在编程中,我们可以使用 toupper()
或 tolower()
函数来简化 switch
语句中的情况判断。例如:
switch(toupper(answer))
{
case 'Y':
printf("You responded in the affirmative.\n");
break;
case 'N':
printf("You responded in the negative.\n");
break;
default:
printf("You did not respond correctly...\n");
break;
}
需要注意的是,如果要使用 toupper()
函数,需要包含 ctype.h
头文件。
2. goto
语句
if
语句能让我们根据测试结果在两个语句块中做出选择,改变程序的执行顺序。而 goto
语句则是一种直接改变语句流向的工具,它会无条件地将程序控制转移到指定的位置。
goto
语句的使用方法如下: