自学内容网 自学内容网

C(九)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
10

3.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变调整👉

教官:“怎么还有在慢慢晃的?!1,3,5,7,9”。

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
int main()
{
printf("怎么还有在慢慢晃的?!1,3,5,7,9\n");
int i = 1; //1、初始化
while (i <= 10)//2、判断
{
printf("%d\n", i);
i += 2;//3、调整
}
return 0;
}

运行结果:

怎么还有在慢慢晃的?!1,3,5,7,9
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*3+3*4*5+5*6*7+······+ 9*10*11 

五、军训记录

1、 匕首操oi~oi~oi~~~

2、《后来》 

 


原文地址:https://blog.csdn.net/2401_87025655/article/details/142651077

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!