当前位置:网站首页>c语言---13 循环语句while
c语言---13 循环语句while
2022-06-10 17:31:00 【要努力丫!】
在学习和回顾该知识前,已经掌握了if语句的结构和用法。
if (条件)
语句;
当条件满足的情况下,if结构中的语句执行,且只执行一次;条件不满足则不执行。但是生活中常常需要将一件事情完成很多次,这时候就要用到while语句来实现循环了。
1、while语句结构
while (表达式)
循环语句;
例子:利用循环结构打印1-10
#include <stdio.h>
int main()
{
int i = 0;
while(i < 10)
{
i++;
printf("%d ", i);
}
return 0;
}
1 2 3 4 5 6 7 8 9 10

来测试一下在代码中分别加入continue和break是什么结果在while循环中,break用于永久地终止循环
int main()
{
int i = 1; //初始化
while (i <= 10) //判断部分
{
if (i == 5)
break;
printf("%d ", i);
i++; //循环变量的调整部分
}
return 0;
}
运行结果为:
1 2 3 4
将break改为continue看看,发现运行结果为:
输出4之后,光标持续闪烁,因为在while循环中,continue的作用是跳过本次循环continue后面的代码(这样就跳过了printf("%d ", i);i++; 这两条语句),直接程序又去到判断部分,看是否进行下一次循环。
2、代码示例
示例一:
看一下下面这段代码是什么意思
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
运行结果如下:
输入什么字母就会输出什么字母,想要退出该程序的运行,就使用快捷键“ctrl + z”。
a
a
A
A
^Z
getchar这个是指如果读取成功的话,返回的是ASCII码值,若读取失败,则返回的是EOF(end of file),是文件结束标志。
测试getchar的功能
int main()
{
int ch = getchar();
printf("%c\n",ch);
//putchar(ch);//这条语句与上一条语句一样的意思
return 0;
}
运行该测试代码,在打印结果的窗口,键入一个A字符回车,就会打印出A字符。(putchar是指输出一个字符)
在getchar和键盘之间有一个缓冲区,键入字符“A”就相当于将“A\n”放进了缓冲区里面。
示例二:
假设输入一段字符作为密码,将该密码存放到字符串passwd里面,然后弹出一个“请确认密码”的提示,如果确认密码正确就键入“Y”,输出“确认成功”;否则键入“N”,输出“确认失败”。编写的代码如下:
int main()
{
char passwd[20] = {
0 };
printf("请输入密码:>");
scanf("%s",passwd);//假设输入的密码是2022
//这里passwd不取地址的原因是passwd是个数组,数组的数组名本身就是个地址,所以
//此处没有给passwd加上取地址符
printf("请确认密码(Y/N):>");
int ch = getchar();
if (ch == 'Y')
{
printf("确认成功\n");
}
else
{
printf("确认失败\n");
}
return 0;
}
运行该代码的结果如下:
请输入密码:>2022
请确认密码(Y/N):>确认失败
显然,该代码是存在错误的。还没等输入Y/N,就提示确认失败了。这是为甚呢?因为我们在键盘上键入“2022”之后,还会键入一个回车,此时放到缓冲区就放的是“2022\n”。
而scanf与getchar是输入函数,它们不是直接从键盘上拿数据,而是从中间的缓冲区去拿数据,如果缓冲区没有数据,它就会等待从键盘上输入一点信息到缓冲区里面去。scanf只会将缓冲区里面的“2022\n”里的“2022”拿走,缓冲区里面还剩下“\n”;getchar在读取的时候,就没有作等待动作,直接将缓冲区里面的“\n”拿走了,所以还么等我们输入Y/N,就已经打印了“确认失败”。
如何修正程序呢?
将执行scanf之后的缓冲区里面的“\n”拿走,也就是将缓冲区清理干净,这样的话getchar函数就得等待输入,就可以正确运行了。
代码如下:
int main()
{
char passwd[20] = {
0 };
printf("请输入密码:>");
scanf("%s",passwd);//假设输入的密码是2022
printf("请确认密码(Y/N):>");
//清理缓冲区
getchar();//处理'\n'
int ch = getchar();
if (ch == 'Y')
{
printf("确认成功\n");
}
else
{
printf("确认失败\n");
}
return 0;
}
运行结果为:
请输入密码:>2022
请确认密码(Y/N):>Y
确认成功
这时候对于连续的密码输入来说是正确运行的,那么对于不连续(中间有空格)的呢?
比如密码是“2022 abc”,执行结果如下:
请输入密码:>2022 abc
请确认密码(Y/N):>确认失败
可以看出,程序还是存在纰漏。那么怎么处理呢?这就要将缓冲区里面的多个字符都给清理干净。此时只一个getchar函数处理不掉空格以及“\n”了,可以使用while循环,让它一直读取,直到将“\n”都读走。
int main()
{
char passwd[20] = {
0 };
printf("请输入密码:>");
scanf("%s",passwd);//假设输入的密码是2022
printf("请确认密码(Y/N):>");
清理缓冲区
//getchar();//处理'\n'
//清理缓冲区中的多个字符
int tmp = 0;
while ((tmp = getchar()) != '\n')
{
;
}
int ch = getchar();
if (ch == 'Y')
{
printf("确认成功\n");
}
else
{
printf("确认失败\n");
}
return 0;
}
请输入密码:>2022 abc
请确认密码(Y/N):>Y
确认成功
示例三:
//只会打印数字
int main()
{
int ch;
while ((ch = getchar()) != EOF)
{
if (ch < '0' || ch > '9')
continue;
putchar(ch);
}
return 0;
}
根据ASCII码值,我们可以知道该段代码的意思是:如果不是数字就continue,执行continue会跳过putchar(ch)这句话,所以这段代码最终只会打印输入的数字,想要终止运行,键入“ctrl+z”即可。代码运行结果如下:
1
1
3
3
9
9
^Z
边栏推荐
- 高数_第6章无穷级数__正项级数的性质
- Wireshark learning notes (I) common function cases and skills
- Abbexa 8-OHdG CLIA 试剂盒解决方案
- Can the "no password era" that apple is looking forward to really come true?
- Canvas大火燃烧h5动画js特效
- 高数_第6章无穷级数__绝对收敛_条件收敛
- Protocol Gen go grpc 'is not an internal or external command, nor is it a runnable program or batch file
- Abbexa 细菌基因组 DNA 试剂盒介绍
- There is an urgent need to enrich the smart home product line. Can fluorite be crowded on the sweeping robot track?
- Mapbox GL development tutorial (11): loading line layers
猜你喜欢

The relationship between trees, forests and binary trees

2022 version of idea graphical interface GUI garbled code solution super detailed simple version

Classic topics of leetcode tree (I)

使用Canvas实现的噪音线条h5js特效

凹印套印原理及影响套印的因素

Mapbox GL development tutorial (11): loading line layers

com. netflix. client. ClientException: Load balancer does not have available server for client: userser

ZigBee模块无线传输星形拓扑组网结构简介

堆利用之chunk extend: HITCON tranining lab13

Nacos configuration management
随机推荐
js模糊阴影跟随动画js特效插件
JS blur shadow follow animation JS special effect plug-in
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
The relationship between trees, forests and binary trees
Mapbox GL development tutorial (11): loading line layers
.NET 开源的免费午餐结束了?
Flutter在数字生活的发展与天翼云盘落地实践
Jouer avec la classe de fonctions de pytorch
Penguin E-sports stops, and tiger teeth are hard to walk
项目中常用的19条MySQL优化
内存池原理一(基于整块)
电商行业转账返款方案分析
yml文件配置参数定义字典和列表
Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
Container containing the most water
解决 vs2022在调试程序时缓慢加载一堆符号的问题
XML&Xpath解析
Unity踩坑记录:如果继承MonoBehaviour,类的构造函数可能会被Unity调用多次,不要在构造函数做初始化工作
sense of security
Canvas fire burning H5 animation JS special effects