关於C 语言中的CONTINUE用法

来源:百度知道 编辑:UC知道 时间:2024/05/25 10:42:00
#include<stdio.h>
#include<ctype.h>
void main()
{
char ch;

printf("Give me a letter of the alphabet, and I will give");
printf("an animal name\n beginning with that letter.\n");
printf("Please type in a letter; type # to end my act.\n");

while((ch=getchar())!='#')
{
if(islower(ch))

switch(ch)
{
case 'a':
printf("argali\n");
break;

case 'b':
printf("bug\n");
break;

case 'c':
printf("cat\n");
break;

case 'd':
printf("dog\n");
break;

default:<

就你这个程序里面,有没有continue;
没区别。
continue;的意思是这次循环里面,continue后面的语句都不执行了。返回循环开头,进行下一次循环

这一句在这的作用是去掉输入中第一个字符以外的其他字符,也就是清空输入缓存(包括回车)。使得下次判断的是下次输入的第一个字符。
continue用在while循环里,作用是遇到continue;无条件的转到while语句,重新判断循环条件。
楼上说的不对,这个程序不写continue会执行若干次printf("Please type a letter or a #.\n");
顺便说一句,有一个函数专门用来清空输入缓存的,貌似是flush(stdin);

while((ch = getchar())!= '\n')
{
continue;
do sth;
}
如果进入循环,就直接跳到 while((ch = getchar())!= '\n')。不会执行 do sth 的。

也就是 continue 是结束本次循环,跳到循环的开头。而break 是直接结束循环。