illegal break && illegal continue

来源:互联网 发布:高三如何提高成绩知乎 编辑:程序博客网 时间:2024/04/28 07:45
我们都知道在C语言中用 break 语句可以提前终止循环;用continue语句可以提前结束本次循环但有时候似乎有点问题。
#include<stdio.h>#include<string.h>#define Maxsize 50void main(){//输入一行字符,分别统计其中的英文字母空格数字和其他字符的个数char str[Maxsize];int num,space,letter,others;printf("Please enter a series of character,end up with '#'\n");for(int i=0;i<Maxsize;i++)scanf("%c",&str[i]);if(str[i] == '#') break;for(int j=0;j<=strlen(str)-1;j++)if(str[i]>=65 && str[i]<=122)  letter++;continue;if(str[i]>=48 && str[i]<=57)   num++;continue;if(str[i] == 32)   space++continue;others++;printf("The amount of letters is %d\n",letter);printf("The amount of numbers is %d\n",num);printf("The amount of spaces is %d\n",space);printf("The amount of other characters is %d\n",others);}


下面我们运行下程序看看功能是否能实现,我们运行后发现,统计字符串的函数根本没有起作用

第十二行 if(str[i] == '#') break; 乍一看是没有错误,仔细一看还是没有错误。那为什么会报错呢?

break和continue只能用于循环语句之中,因此为了能让编译系统识别出它们是在循环体中需要加上将循环体用花括号括起来,如下图所示

这样编译就可以顺利通过了。

                                             
0 0
原创粉丝点击