文章目录
循环语句
while语句
for语句
do while语句
1.while语句
1.1 语法
while语句的结构时怎么样的呢?
//while 语法结构
while(表达式)
循环语句;
代码演示
#include <stdio.h>
int main()
{
while(1)
printf("hehe");
return 0;
}
打印结果为死循环打印hehe
用while循环实现
在屏幕上打印1-10的数字
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i++;;
}
return 0;
}
1.2 while语句中的break和continue
break介绍
//break 代码实例
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
break;
printf("%d ", i);
i++;
}
return 0;
}
打印结果为1 2 3 4
总结:
break在while循环中的作用:
其实在循环中只要遇到break,就停止后期的所有的循环,直接终止循环。
所以:while中的break是用于永久终止循环的。
continue介绍
//continue 代码实例1
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
continue;
printf("%d ", i);
i++;
}
return 0;
}
打印结果为1 2 3 4死循环
总结
continue在while循环中的作用就是:
continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再行,
而是直接跳转到while语句的判断部分。进行下一次循环的入口判断
1.3 getchar与putchar用法
输入输出
scanf / printf
getchar — 读取一个字符
putchar — 输出打印一个字符
getchar 读取到字符的时候返回的是字符的ASCII码值
如果读取失败,返回的是EOF -----> -1
代码演示
getchar()函数的返回值是int,所以要用int来接收
#include <stdio.h>
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
这里的代码适当的修改是可以用来清理缓冲区的.
举例说明
当输入ABCDEF时后边没有等待直接确认失败,那是因为scanf只读取了ABCDEF,而实际上还有一个\n,所以此时需要用getchar来清理缓存区
所以修改后如下
可是修改后我又发现,当输入123456 abc时又是确认失败
那是因为scanf只读走123456,而缓冲区还剩下多个字符,getchar()只能读取一个字符,所以确认失败。
那要怎么解决这个问题呢?
只需要给我们这个代码加一个while循环即可,当缓冲区被清空后则跳出while循环
#include <stdio.h>
int main()
{
printf("请输入密码:>");
char password[20] = {
0 };
scanf("%s", password);
while (getchar() != '\n')
{
;
}
printf("请确认密码(Y/N):>");
int ch = getchar();
if (ch == 'Y')
{
printf("确认成功\n");
}
else
{
printf("确认失败\n");
}
return 0;
}
1.4 练习
#include <stdio.h>
int main()
{
char ch = '\0';
while ((ch = getchar()) != EOF)
{
if (ch < '0'|| ch > '9')
continue;
putchar(ch);
}
return 0;
}
这个代码的作用是:只打印数字字符,跳过其他字符
2.for循环
2.1 语法
for(表达式1; 表达式2; 表达式3)
循环语句;
表达式1 表达式1为初始化部分,用于初始化循环变量的。
表达式2 表达式2为条件判断部分,用于判断循环时候终止。
表达式3 表达式3为调整部分,用于循环条件的调整。
代码演示:
- 使用for循环 在屏幕上打印1-10的数字。
#include <stdio.h>
int main()
{
int i = 0;
//for(i=1/*初始化*/; i<=10/*判断部分*/; i++/*调整部分*/)
for(i=1; i<=10; i++)
{
printf("%d ", i);
}
return 0;
}
可以发现for循环中依然存在while循环中的三个必须条件,但是由于风格的问题使得while循环中三个部分很可能偏离较远,这样查找修改就不够集中和方便。所以,for循环的风格更胜一筹;for循环使用的频率也最高。
2.2 for循环中的break和continue
我们发现在for循环中也可以出现break和continue,他们的意义和在while循环中是一样的。
但是还是有些差异:
break介绍
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
if(i == 5)
break;
printf("%d ",i);
}
return 0;
}
打印结果为1 2 3 4
总结
由此可见,break在while和for循环中作用一样,都是永久终止循环的
continue介绍
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
if(i == 5)
continue;
printf("%d ",i);
}
return 0;
}
打印结果为1 2 3 4 6 7 8 9 10
总结
for循环中的continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再行,而是直接跳转到for语句的调整部分。进行下一次循环的入口判断
2.3 for语句的循环控制变量
建议:
- 不可在for 循环体内修改循环变量,防止 for 循环失去控制。
- 建议for语句的循环控制变量的取值采用“前闭后开区间”写法。
int i = 0;
//前闭后开的写法
for(i=0; i<10; i++)
{
}
//两边都是闭区间
for(i=0; i<=9; i++)
{
}
2.4 一些for循环的变种
for循环中的初始化部分,判断部分,调整部分是可以省略的,如果把判断部分省略,就意味着判断恒为真。
但是不建议初学时省略,容易导致问题
//代码1
#include <stdio.h>
int main()
{
//代码1
for(;;)
{
printf("hehe\n");
}
return 0;
}
//代码2
int i = 0;
int j = 0;
//这里打印多少个hehe?
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("hehe\n");
}
}
//代码3
int i = 0;
int j = 0;
//如果省略掉初始化部分,这里打印多少个hehe?
for(; i<10; i++)
{
for(; j<10; j++)
{
printf("hehe\n");
}
}
这里我们发现代码2打印9个hehe,代码3打印3个hehe。这是为什么呢?
只有在i=0的时候j打印三次hehe,当i=1和2时,j还是等于3,所以j不执行,hehe只打印三次。
//代码4-使用多余一个变量控制循环
int x, y;
for (x = 0, y = 0; x<2 && y<5; ++x, y++)
{
printf("hehe\n");
}
return 0;
}
for循环里边可以是一个变量也可以是两个或多个变量。
2.5 练习
//请问循环要循环多少次?
#include <stdio.h>
int main()
{
int i = 0;
int k = 0;
for(i =0,k=0; k=0; i++,k++)
k++;
return 0;
}
一次循环都不会进入
因为for循环的判断部分为k=0,而’ = ’ 为赋值,k赋值为0,0为假所以一次循环也不进入。
注: 切记’ = ‘为赋值,’ == '为等于。
3.do…while()循环
3.1 语法
do
循环语句;
while(表达式);
3.2 执行流程图
代码演示:
在屏幕上打印1~10
#include <stdio.h>
int main()
{
int i=1;
do
{
printf("%d",i);
i++;
}while(i<=10);
return 0;
}
循环至少执行一次,使用的场景有限,所以不是经常使用。
3.3 do while循环中的break和continue
#include <stdio.h>
int main()
{
int i = 1;
do
{
if(5 == i)
break;
printf("%d ", i);
i++;
}while(i<=10);
return 0;
}
打印结果为1 2 3 4
#include <stdio.h>
int main()
{
int i = 1;
do
{
if(5 == i)
continue;
printf("%d ", i);
i++;
}while(i<=10);
return 0;
}
打印结果为1 2 3 4死循环
总结
breaak:for , while , do----while , switch语句中
continue:for , while , do----while 语句中
4.循环练习
4.1 n的阶乘
//不考虑溢出
#include <stdio.h>
int main()
{
int n=0;
scanf("%d",&n);
int i=0;
int ret=1;
for(i=1;i<=n;i++)
{
ret*=i;
}
printf("%d", ret);
return 0;
}
4.2 计算 1!+2!+3!+……+10!
代码一
#include <stdio.h>
int main()
{
int n=0;
int i=0;
int ret=1;
int sum=0;
for(n=1;n<=10;n++){
ret=1;
for(i=1;i<=n;i++)
{
ret*=i;
}
sum+=ret;
}
printf("%d", sum);
return 0;
}
代码二
//优化版
#include <stdio.h>
int main()
{
int n=0;
int ret=1;
int sum=0;
for(n=1;n<=10;n++){
ret*=n;
sum+=ret;
}
printf("%d", sum);
return 0;
}
4.3 编写代码,演示多个字符从两端移动,向中间汇聚
定义两个数组,通过第一个的左下标找一个元素放在第二个数组的左下标相同位置上,在通过右下标找一个元素放在第二个
代码一
//while循环
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main()
{
char arr1[] = "welcome to bit!!!!";
char arr2[] = "******************";
int left = 0;
int right = strlen(arr1)-1;//strlen()求字符串长度
//下标从零开始,而元素是个数,所以strlen(arr1)-1
while (left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(1000);
//单位是毫秒,头文件为#include <windows.h>
system("cls");
//system()为命令执行函数,cls是清空屏幕
left++;
right--;
}
printf("%s\n", arr2);
return 0;
}
代码二
//for循环
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main()
{
char arr1[] = "welcome to bit!!!!";
char arr2[] = "******************";
int left = 0;
int right = 0;
for (left = 0, right = strlen(arr1) - 1; left <= right; left++, right--)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(1000);
system("cls");
}
printf("%s\n", arr2);
return 0;
}
4.4 模拟用户登录情景
编写代码实现,模拟用户登录情景,并且只能登录三次。(只允许输入三次密码,如果密码正确则 提示登录成,如果三次均输入错误,则退出程序。
#include <string.h>
#include <stdio.h>
int main()
{
int i = 0;
char password[20] = {
0 };
int flag = 0;
for (i = 0; i < 3; i++)
{
printf("请输入密码:>");
scanf("%s", password);
//判断
if (strcmp(password, "123456") == 0)
//strcmp()判断字符串是否相等,如果相等返回0
{
flag = 1;
printf("密码正确\n");
break;
}
else
{
printf("密码错误\n");
}
}
if (flag == 0)
printf("三次密码均错误,退出程序\n");
return 0;
}