while循环结构
只有当给定的条件为真时,C语言中的while循环语句会重复执行一个目标语句;
实列:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=0;
while(a!=10)//a的判断条件;
{
printf("a的值:%d\n",a);
a++;
}
system("pause");
return 0;
}
while(条件语句)
{
目标语句;
}
当条件语句为真时,C语言中的while语句会重复执行{ }中的目标语句,直到条件语句判断为假时,会跳出或者是说会终止while循环;
while循环结构例题:输入一串字符串判断字符串中的字母,数字,空格,和其他符号的个数:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a[100];//定义大小为100的字符串数组;
int b = 0, c = 0, i = 0,d = 0,e = 0;
gets(a);//用gets输入字符串;
while(a[i]!='\0')//给定判断条件,当字符串数组不等于\0时为真,否则跳出;
{
if ('A' <= a[i] && a[i] <= 'Z' || 'a' <= a[i] && a[i] <= 'z')
b++;
else if ('0' <= a[i] && a[i] <= '9')
c++;
else if (a[i] == ' ')
d++;
else
e++;
i++;
}
printf("\n*******************\n");
printf("字母:%d\n",b);
printf("数字:%d\n",c);
printf("空格:%d\n",d);
printf("其他符号:%d\n",e);
printf("---------------------\n");
system("pause");
return 0;
}