一. 格式:
1: while (条件)
2:
3: {
4:
5: 循环体
6:
7: }
8:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
二.运行原理
1.如果一开始条件就不成立,永远不会执行循环体
2.如果条件成立,就会执行一次循环体,执行完毕,再次判断条件是否成立......
三.while常用的两个语句:break和continue
1: int count = 0;
2:
3: while (count<50)
4:
5: {
6:
7: ++count;
8:
9: if (count%10 != 0)
10:
11: {
12:
13: printf("做第%d次俯卧撑\n", count);
14:
15: }
16:
17: }
18:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1.break,直接结束整个while循环
1: int count=0;
2:
3: while (count < 50)
4:
5: {
6:
7: ++count;
8:
9: printf("做第%d次俯卧撑\n", count);
10:
11: if (count == 20)
12:
13: {
14:
15: break;
16:
17: }
18:
19: }
20:
21: return 0;
22:
23: }
24:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2.continue,结束当前的循环体,进入下一次循环体的执行
1: int count=0;
2:
3: while (count<50)
4:
5: {
6:
7: ++count;
8:
9: if (count%10 == 0)
10:
11: {
12:
13: // 直接结束这一次循环体,进入下一次循环
14:
15: continue;
16:
17: }
18:
19: printf("做第%d次俯卧撑\n", count);
20:
21: }
22:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
四.while语句常见死循环
1.最简单的死循环
1: while(1);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2. 永无止境的”哈哈哈”
1: while (10)
2:
3: {
4:
5: printf("哈哈哈哈\n");
6:
7: }
8:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
五.while应用小例子
1: /*
2:
3: 提示用户输入一个正整数n,计算1+2+3+…+n的和
4:
5: */
6:
7: #include <stdio.h>
8:
9: int main()
10:
11: {
12:
13: // 1.提示输入
14:
15: printf("请输入一个正整数:\n");
16:
17: // 2.接收输入
18:
19: // 定义变量保存用户输入的整数
20:
21: int n;
22:
23: scanf("%d", &n);
24:
25: if (n<=0)
26:
27: {
28:
29: printf("非法输入\n");
30:
31: return 0;
32:
33: }
34:
35: // 3.计算
36:
37: // (1 + n) * n / 2;
38:
39: // 定义变量保存和
40:
41: int sum = 0;
42:
43: int number = 0; // 默认被加的数值
44:
45: while (number < n)
46:
47: {
48:
49: number++;
50:
51: sum += number; // 累加
52:
53: }
54:
55: printf("%d\n", sum);
56:
57: return 0;
58:
59: }
60:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }