C语言循环结构

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;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值