1.break 和continue
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int i = 1;
while (i<=10)
{
if (i == 5)
break;
printf("%d \n", i);
i++;
}
return 0;
}
1
2
3
4
把其中的break;换成continue;答案是1234之后无限继续
1.只有遇到break,就停止后面的所有循环,直接终止循环。
2.continue用于终止本次循环,本次循环continue后面的代码不会执行,而是直接跳转到while的判断部分,进行下一次循环的入口判定。
2.getchar, EOF
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int ch = getchar();
putchar(ch);
printf("%c\n", ch);
printf("----------------------\n");
int ch1 = 0;
//CTRL + Z 能停止循环
while ((ch1 = getchar()) != EOF)//EOF --end of file 值是-1
{
putchar(ch1);
}
return 0;
}
s
ss
----------------------
d
d
dea
dea
sdad
sdad
^Z
...................... (进程 15316)已退出,代码为 0。
按任意键关闭此窗口. . .
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int ret = 0;
char password[20] = { 0 };
printf("请输入密码: ");
scanf("%s", password);//输入密码并存入password数组中
printf("请输入(Y/N) ");
//缓冲区还剩余一个'\n'(回车输入的)
//循环读取直到读取到'\n'
int ch = 0;
while ((ch=getchar()) != '\n')
{
;
}
ret = getchar();// Y/N
if (ret == 'Y')
{
printf("确认成功\n");
}
else {
printf("放弃确认\n");
}
return 0;
}
请输入密码: 123456 asdada asdas
请输入(Y/N) Y
确认成功
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int ch = 0;
while ((ch=getchar()) != EOF)
{
//字符'0'到'9'可以再输出来,其他不行
if (ch < '0' || ch>'9')
continue;
putchar(ch);
}
return 0;
}
0
0
8
8
9
9
aasd
asd
fr
dsrg
^Z