网站显示结算,必应收录提交入口,微信平板专用版ipad版,ppt模板免费下载完整版免费无需会员匕首操#xff0c;oi~oi~oi~~~~~ 接下来的几篇推文#xff0c;杰哥记录的是三大循环结构的运行流程及其变式。 本篇的主角是while循环。#x1f449; 目录#xff1a; while循环 的组成、运行流程及其变式关键字break 和 continue 在while 循环中的作用while 循环的嵌套题目… 匕首操oi~oi~oi~~~~~ 接下来的几篇推文杰哥记录的是三大循环结构的运行流程及其变式。 本篇的主角是while循环。 目录 while循环 的组成、运行流程及其变式关键字break 和 continue 在while 循环中的作用while 循环的嵌套题目收集军训记录 一、while循环 的组成、运行流程及其变式 场景一 教官 ———“我数到十马上集合快点”。代码示例 #define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{printf(我数到10马上集合\n);int i 1; //1、初始化while (i 10)//2、判断{printf(%d\n, i);i;//3、调整}return 0;
} 1、while 的三大组成
———— 1、初始化 2、判断3、调整
2、while 的运行流程
break 和 continue可以先不用管先忽视它。 3、变式
改动其中任意一个结果可能会受到影响甚至死循环。
3.1变初始化 教官“我从3开始数”。
#define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{printf(我数到10马上集合\n);int i 3; //1、初始化while (i 10)//2、判断{printf(%d\n, i);i;//3、调整}return 0;
} 运行结果
我数到10马上集合
3
4
5
6
7
8
9
103.2 变判断
教官“我只数到9”
#define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{printf(我只数到9马上集合\n);int i 1; //1、初始化while (i 9)//2、判断{printf(%d\n, i);i;//3、调整}return 0;
}运行结果
我只数到9马上集合
1
2
3
4
5
6
7
8
9
3.3变调整
教官“怎么还有在慢慢晃的13579”。
#define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{printf(怎么还有在慢慢晃的13579\n);int i 1; //1、初始化while (i 10)//2、判断{printf(%d\n, i);i 2;//3、调整}return 0;
}
运行结果
怎么还有在慢慢晃的13579
1
3
5
7
9
3.4都改变
教官倒数。
#define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{int i 10; //1、初始化while (i 1)//2、判断{printf(%d\n, i);i--;//3、调整}return 0;
}
运行结果
10
9
8
7
6
5
4
3
2
1
二、关键字break 和 continue 在while 循环中的作用
1、break
作用打破循环。 场景二 教官“从前往后报数直到报到8的同学那停止” #define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{int i 1; //1、初始化while (1)//2、判断--1表示真那么while将进入死循环直到遇到break等等{printf(%d\n, i);if (8 i){break;}i;//3、调整}return 0;
}
运行结果
1
2
3
4
5
6
7
8
2、continue
作用跳过本次循环continue后面的部分。而break是跳过整个循环 场景三 教官“从前往后报数把没来的同学空出来”。 #define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{int i 0; //1、初始化while (i 10)//2、判断{i;//3、调整if (5 i){continue;}printf(%d\n, i);}return 0;
}当i 4进入循环通过i变成5进入if语句中但是遇到continue所以回到循环的判断部分
i 5符合条件进入循环通过i后变成6这样就不用再遇到continue接下来照常运行。
运行结果没有5
1
2
3
4
6
7
8
9
10
三、while 循环的嵌套 场景四 打印匕首操集合队形30 * 20的方阵 分析对于队形的打印除了列还有行。接着确定循环嵌套的内外层内层不妨为行的打印外层不妨为列的控制。 #define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{int i 0; while (i 20){int j 0;while (j 30){printf(* );j;}printf(\n);i;}return 0;
}
外层循环每进行一次内层就要完整进行完一次。运行结果
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 场景五 打印一个三角形方阵 分析第一行只有一列第二行有两列······行与列有一定的联系。
因此判断部分改成 j i
#define _CRT_SECURE_NO_WARNINGS 1#include stdio.h
int main()
{int i 0; while (i 20){int j 0;while (j i)//改变判断部分即可做到{printf(* );j;}printf(\n);i;}return 0;
}
运行结果
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
四、题目收集 4.1计算1 2 3 4 5 ······ 10 4.2计算1*2*33*4*55*6*7······ 9*10*11 五、军训记录
1、 匕首操oi~oi~oi~~~ 2、《后来》