- 循环辅助continue and break
一般而言,程序在进入循环后,在进行性下一次循环前,会执行完本次循环体中的所以语句。continue和break语句可以根据循环体中的测试结果来忽略一部分循环体内容,甚至结束循环
continue语句
当循环执行到continue语句时,会跳过本次迭代的剩余部分,并开始下一轮迭代。如果continue嵌套在循环内,则只会影响包含该语句的内层循环。
/*skippart.c -- 使用continue跳过部分循环*/
#include <stdio.h>
int main (void)
{
const float MIN = 0.0f;
const float MAX = 100.0f;
float score;
float total = 0.0f;
int n = 0;
float min = MAX;
float max = MIN;
printf("Enter the first score (q to quit):");
while(scanf("%f", &score) == 1){
if (score < MIN || score > MAX){
printf("%0.1f is an invalid value. Try again: ",score);
continue; //跳转至while循环的测试条件
}
printf("Enter next score (q to quit):");
}
if (n > 0){
printf("Average of %d score is %0.1f .\n", n, total / n);
printf("Low = %0.1f, high = %0.1f\n", min, max);
}else{
printf("No valid score were entered.\n");
}
return 0;
}
break语句
当程序执行到break语句时,会终止包含它的循环,并继续执行下一个阶段。如果把上面的程序中的continue换成break,那么当执行break语句时,会直接退出整个循环,而不是退出本次循环。
break还可以用于其他原因退出循环的情况。
比如下面的程序,计算一个矩形的面积,当输入非数字作为矩形的长和宽时,退出循环
/*break.c -- 使用break退出循环*/
#include <stdio.h>
int main (void)
{
float length, width;
printf("Enter the length of the rectangle(q to quit):\n");
while(scanf("%f", &length) == 1){
printf("Length = %0.2f\n", length);
printf("Enter its width:\n");
if(scanf("%f", &width) != 1){
break;
}
printf("Width = %0.2f\n", width);
printf("Area = %0.2f\n", length * width);
printf("Enter the length of the rectangle(q to quit):\n");
}
printf("Done!\n");
return 0;
}
Enter the length of the rectangle(q to quit):
4
Length = 4.00
Enter its width:
2
Width = 2.00
Area = 8.00
Enter the length of the rectangle(q to quit):
q
Done!
其实,在这些程序中用来continue和break反而让程序更加复杂了,有时用逻辑运算符加入其中,这样程序会更加简洁。
嵌套内层的break,只能跳出内层循环,跳出外层还需要一个break。
多重选择:switch and breaks
使用条件运算符和if语句很容易编写二选一的程序。然而,有时选项不止两个,虽然这种情况下可以使用if else嵌套,但大多数情况下还是switch语句更加方便些。
/*animals_switch.c -- 使用switch语句*/
#include <stdio.h>
#include <ctype.h>
int main (void)
{
char ch;
printf("Give me a letter of the alphabet, and I will give an animal name.\n");
printf("Please type in a letter; type # to end my act.\n");
while((ch = getchar()) != '#'){
if ('\n' == ch){
continue;
}
if (islower(ch)) //直接收小写字母
switch (ch){
case 'a':
printf("argali, a wild sheep of Asia\n");
break;
case 'b':
printf("babirusa, a wild pig of Malay\n");
break;
case 'c':
printf("coati, racoonlike mammal\n");
break;
case 'd':
printf("desman, aquatic, molelike critter\n");
break;
case 'e':
printf("echidna, the spiny anteater\n");
break;
case 'f':
printf("fisher, brownish marten\n");
break;
default:
printf("That is a stumper!\n");
}
else
printf("I recongize only lowercase letters.\n");
while(getchar() != '\n'){
continue;
}
printf("Please type another letter or a #.\n");
}
printf("Bye!\n");
return 0;
}
运行一下:
Give me a letter of the alphabet, and I will give an animal name.
Please type in a letter; type # to end my act.
a
argali, a wild sheep of Asia
Please type another letter or a #.
e
echidna, the spiny anteater
Please type another letter or a #.
#
Bye!
书上就给了这几个选项,其实还可以更多,格式都一样。
switch语句
分析一下switch语句的工作原。switch语句中,要对紧跟着switch后面圆括号里的值求值,就像上面的程序中,赋值一个小写字母给ch,再由ch将其代入switch中。在switch中进行检测(程序扫描标签,即止case这一行的内容),如果检测到匹配的就执行该语句,如果没检测到匹配的,全都去执行default后面的语句。
可以注意到,在每条case语句后面都有一个break,break使得程序能跳出switch并进行下面的程序。试想如果没有了break将会出现什么。我们将上一个程序中的break去掉并运行后,得到的是这样的结果:
Give me a letter of the alphabet, and I will give an animal name.
Please type in a letter; type # to end my act.
a
argali, a wild sheep of Asia
babirusa, a wild pig of Malay
coati, racoonlike mammal
desman, aquatic, molelike critter
echidna, the spiny anteater
fisher, brownish marten
That is a stumper!
Please type another letter or a #.
执行了所有语句。值得注意的还有,程序中还用了一种输入方式,使得程序只读每行的首字母。可以输入abc来进行验证:
Give me a letter of the alphabet, and I will give an animal name.
Please type in a letter; type # to end my act.
abc
argali, a wild sheep of Asia
Please type another letter or a #.
在输入abc后,程序将bc忽略了原因在于程序中的
while(getchar() != '\n'){
continue;
}
这段语句可以让程序跳过输入行的其他部分。同时如果在一开始输入的是空格,同样不会运行程序的正常运行,因为有
if ('\n' == ch){
continue;
}
多重标签
多重标签就像这样:
case 'a':
case 'A':
printf("argali, a wild sheep of Asia\n");
break;
两个case叠在一起就可以实现,输入a和输入A的结果是一样的。
switch和if else
到底选用那一个?二者的功效好像差不多,但在有些时候会体现出差异,比如如果用浮点型的变量或表达式来选择,就无法使用switch;如果有一个范围,比如1到1000,switch就会很麻烦,要列出1000个case,而if就很轻松。各有各的长处,就看要求是什么样的。