当前位置:网站首页>C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句
C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句
2022-07-26 22:49:00 【Lydialyy】
目录
一、while语句
while(表达式)
循环体
1.代码实战一:用C语言计算1+2+3+……+100的结果
代码如下:
#include <stdio.h>
int main()
{
int i = 1,sum = 0;
while(i <= 100)
{
sum = sum + i;
i = i + 1;
}
printf("结果是:%d\n",sum);
return 0;
}运行结果:

getchar函数
(1)函数摘要:getchar函数从标准输入流(stdin)中获取下一个字符。相当于调用getc(stdin)函数。
(2)函数原型:
#include <stdio.h>
…
int getchar()(void);(3)返回值:
如果函数调用成功,返回获取的字符(用整型表示其ASCII码)
返回值如果是EOF,表示该函数调用失败;
- 如果标准输入流位于处于结束的位置,该函数返回EOF,并设置标准输入流的结束标志符。
- 如果出现其他错误,该函数同样返回EOF,并设置错误标志符代替。
2.代码实战二:统计从键盘输入的一行英文句子的字符个数
代码如下:
#include <stdio.h>
int main()
{
int count = 0;
printf("请输入一行英文字符:\n");
while(getchar() != '\n')
{
count = count + 1;
}
printf("您总共输入了%d个字符\n",count);
return 0;
}运行结果:

二、dowhile语句
do
循环体
while(表达式);
实际运用,如:验证用户密码是否正确
- 若用while语句,流程图如下:

若用dowhile语句,流程图如下:

while与dowhile语句的区别:

三、for语句和循环结构
1.形式
for(表达式1;表达式2;表达式3)
循环体三个表达式用分号隔开,其中:
- 表达式1是循环初始化表达式
- 表达式2是循环条件表达式
- 表达式3是循环调整表达式
2.打印10次LoveC
代码如下:
#include <stdio.h>
int main()
{
int count;
for(count = 0;count < 10;count++)
{
printf("LoveC!\n");
}
return 0;
}运行结果:

3.写一个程序:判断一个数是否为素数
提示:
素数:在大于1的自然数中,除了1和此数自身外,无法被其他自然数整除的数。
素数的求法之一:迭代测试从2到num/2所有整数是否能被整除(num为待测试的整数),如果没有出现能被整除的整数,那么它就是素数。
代码如下:
#include <stdio.h>
int main()
{
int i,num;
_Bool flag = 1;
printf("请输入一个整数:\n");
scanf("%d",&num);
for(i = 2;i < num / 2;i++)
{
if(num % i == 0)
{
flag = 0;
}
}
if(flag)
{
printf("%d是一个素数!\n",num);
}
else
{
printf("%d不是一个素数!\n",num);
}
return 0;
}运行结果:

4.for语句的灵活运用
(1)表达式1,表达式2和表达式3可以按照需要进行省略(但分号不能省):
- for( ;表达式2 ;表达式3)
- for( 表达式1 ;表达式2 ; )
- for( 表达式1 ; ; )
- for( ; ; )
- ……
(2)表达式1和表达式3可以是一个简单的表达式,也可以是逗号表达式(即用逗号分隔多个表达式)
代码举例:
#include <stdio.h>
int main()
{
int i,j;
for(i = 0,j = 10;i < j; i++,j--)
{
printf("%d\n",i);
}
return 0;
}运行结果:

补充:C99新标准
C99允许在for语句的表达式1中定义变量,如:
for (int i = 0; i < 100; i++)
即上面例子的代码可写为:
#include <stdio.h>
int main()
{
for(int i = 0,j = 10;i < j; i++,j--)
{
printf("%d\n",i);
}
return 0;
}5.循环嵌套
代码举例:
#include <stdio.h>
int main()
{
int i,j;
for(i = 0;i < 3;i++)
{
for(j = 0;j < 3;j++)
{
printf("i = %d,j = %d\n",i,j);
}
}
return 0;
}运行结果:

6.打印九九乘法表
代码如下:
#include <stdio.h>
int main()
{
int i,j;
for(i = 1;i <= 9;i++)
{
for(j = 1;j <= i;j++)
{
printf("%d*%d=%-2d ",i,j,i*j);
}
putchar('\n');
}
return 0;
}运行结果:

四、break语句和continue语句
1.break语句
负责跳出它所在那层语句的循环,若想跳出外层循环还需再布置一个break语句。
同样是上述素数的那个例子,若输入的数字是100000000,在添加了break语句后,执行效率大大增加,代码如下:
#include <stdio.h>
int main()
{
long long int i,num;
_Bool flag = 1;
printf("请输入一个整数:\n");
scanf("%lld",&num);
for(i = 2;i < num / 2;i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag)
{
printf("%d是一个素数!\n",num);
}
else
{
printf("%d不是一个素数!\n",num);
}
printf("i = %lld\n",i);
return 0;
}运行结果:

可见,只需执行2次即可得出结果,但若将break语句注释掉,则执行效率大大降低,运行结果如下:

2.continue语句
当执行continue语句时,循环体的剩余部分将被忽略,直接进入下一次的循环。
代码举例:
#include <stdio.h>
int main()
{
int ch;
while((ch = getchar()) != '\n')
{
if(ch == 'C')
{
continue;
}
putchar(ch);
}
putchar('\n');
return 0;
}运行结果:

注意
- 对于嵌套语句,break语句和continue语句都只能作用于一层循环!
- for语句和while语句并不是完全等价的,它们在执行起来是有区别的:出现continue语句时,while是跳过循环体后边的语句,直接进入下一次的循环。
边栏推荐
- 【mysql】mysql启动关闭命令以及一些报错解决问题
- Unity Huatuo example project source code analysis and inspiration
- Text to image paper intensive reading rat-gan: recursive affine transformation for text to image synthesis
- JS——初识JS、变量的命名规则,数据类型
- JS max?
- 解决方案:读取两个文件夹里不同名的文件,处理映射不对应的文件
- NAT(网络地址转化协议)
- 预分频值和自动重装值对中断频率的影响
- 关于在VS2022或者高级版本运行环境下遇到fopen,strerror等不安全的问题
- 静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。
猜你喜欢
随机推荐
Specify that SQL only supports select syntax
超出隐藏显示省略号
[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram
6.30 written examination of MediaTek
[explain C language in detail] takes you to play with loop structure (for_while_do while)
JS -- first understand the naming rules and data types of JS and variables
HCIA(网络初级综合实验练习)
Unity Huatuo example project source code analysis and inspiration
解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(一)
动态路由ofps协议配置
Enumerated valueof() method stepping on the pit
Flink1.13.6 detailed deployment method
OSPF静态大实验
Is index reproduction text generation image is score quantitative experiment whole process reproduction inception score quantitative evaluation experiment step on the pit and avoid the pit process
count(*)为什么很慢
6.29 Zhong'an Summer Internship
[FPGA tutorial case 30] DDS direct digital frequency synthesizer based on FPGA -- frequency accuracy analysis with MATLAB
Talking about server virtualization & hyper convergence & Storage
Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
7.13 Weilai approved the written examination in advance

![[explain C language in detail] this article takes you to know C language and makes you impressed](/img/37/205c1c6eb2ba704941e48ff89c6268.png)







