循环就是同一件事情完成很多次。
常见的循环语句有:
- while
- for
- do while
以下是本篇文章正文内容,下面案例可供参考
一、当型循环语句——while语句
1.1 语法结构
while(表达式)
循环语句;
1.2 执行流程
while循环按照判断->执行的顺序进行的。
比如我们实现在屏幕上打印1-10的数字。
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0;
}
上面的代码已经帮我了解了 while 语句的基本语法,那我们再了解一下:
1.3 while语句中的break和continue
1.3.1 break介绍
//break 代码实例
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
break;
printf("%d ", i);
i = i+1;
}
return 0;
}
输出结果:
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10
总结:
break在while循环中的作用:
其实在循环中只要遇到break,就停止后期的所有的循环,直接终止循环。
所以:while中的break是用于永久终止循环的。
1.3.2 continue介绍
//continue 代码实例1
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
continue;
printf("%d ", i);
i = i+1;
}
return 0;
}
输出结果:
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10
//continue 代码实例2
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
i = i+1;
if(i == 5)
continue;
printf("%d ", i);
}
return 0;
}
输出结果:
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10
2 3 4 6 7 8 9 10
总结:
continue在while循环中的作用就是:
continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再执行,
而是直接跳转到while语句的判断部分。进行下一次循环的入口判断。
再看几个代码:
//代码什么意思?
//代码1
#include <stdio.h>
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
//这里的代码适当的修改是可以用来清理缓冲区的.
//代码2
#include <stdio.h>
int main()
{
char ch = '\0';
while ((ch = getchar()) != EOF)
{
if (ch < ‘0’ || ch > ‘9’)
continue;
putchar(ch);
}
return 0;
}
//这个代码的作用是:只打印数字字符,跳过其他字符的、
二、当型循环语句——for语句
我们已经知道了while循环,但是我们为什么还要一个for循环呢?
首先来看看for循环的语法结构:
2.1 语法结构
for(表达式1; 表达式2; 表达式3)
循环语句;
表达式1:
表达式1为初始化部分,用于初始化循环变量的。
表达式2:
表达式2为条件判断部分,用于判断循环时候终止。
表达式3:
表达式3为调整部分,用于循环条件的调整。
注意:
- for循环之后不存在“;”符号。
- for循环按照判断->执行的顺序进行。
2.2 代码示例
//使用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;
}
2.3 执行流程
现在我们对比一下for循环和while循环。
#include <stdio.h>
int main()
{
int i = 0;
//实现相同的功能,使用while
i=1;//初始化部分
while(i<=10)//判断部分
{
printf("hehe\n");
i = i+1;//调整部分
}
//实现相同的功能,使用while
for(i=1; i<=10; i++)
{
printf("hehe\n");
}
return 0;
}
可以发现在while循环中依然存在循环的三个必须条件,但是由于风格的问题使得三个部分很可能偏离较远,这样查找修改就不够集中和方便。所以,for循环的风格更胜一筹;for循环使用的频率也最高。
2.4 break和continue在for循环中
我们发现在for循环中也可以出现break和continue,他们的意义和在while循环中是一样的。
但是还是有些差异:
//代码1
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
if(i == 5)
break;
printf("%d ",i);
}
return 0;
}
//代码2
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i++)
{
if(i == 5)
continue;
printf("%d ",i);
}
return 0;
}
2.5 for语句的循环控制变量
建议:
- 不可在for 循环体内修改循环变量,防止 for 循环失去控制。
- 建议for语句的循环控制变量的取值采用“前闭后开区间”写法。
int i = 0; //前闭后开的写法 for(i=0; i<10; i++) {} //两边都是闭区间 for(i=0; i<=9; i++) {}
2.6 一些for循环的变种
#include <stdio.h>
int main()
{
//代码1
for(;;)
{
printf("hehe\n");
}
//for循环中的初始化部分,判断部分,调整部分是可以省略的,但是不建议初学时省略,容易导致问题。
//代码2
int i = 0;
int j = 0;
//这里打印多少个hehe?
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
printf("hehe\n");
}
}
//代码3
int i = 0;
int j = 0;
//如果省略掉初始化部分,这里打印多少个hehe?
for(; i<10; i++)
{
for(; j<10; j++)
{
printf("hehe\n");
}
}
//代码4-使用多余一个变量控制循环
int x, y;
for (x = 0, y = 0; x<2 && y<5; ++x, y++)
{
printf("hehe\n");
}
return 0;
}
三、do...while()循环
3.1 语法结构:
do
循环语句;
while(表达式);
3.2 执行流程
3.3 do语句的特点
循环至少执行一次,使用的场景有限,所以不是经常使用。
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d\n", i);
}while(i<10);
return 0;
}
3.4 do while循环中的break和continue
//代码1
#include <stdio.h>
int main()
{
int i = 10;
do
{
if(5 == i)
break;
printf("%d\n", i);
}while(i<10);
return 0;
}
//代码2
#include <stdio.h>
int main()
{
int i = 10;
do
{
if(5 == i)
continue;
printf("%d\n", i);
}while(i<10);
return 0;
}
值得注意的是,循环体的执行次数最少是一次,每次执行完循环后会检查,如果满足继续循环的条件,则继续循环。do-while循环是先执行后判断。
四、循环的嵌套
1.在c语言中,编写代码可以在一个进行的循环内使用另一个循环。
例:
for(...)
{
for(...)
{
if(disaster)}
}
2 .c语言中嵌套while循环语句的写法。
例:
while(condition)
{
while(condition)
{
if(disaster)
}