C语言学习【C控制语句:循环】
while循环
/* 根据用户键入的整数求和 */#include "stdio.h"int main(void)
{long num;long sum = 0L; /* 把sum初始化为0 */int status;printf("Please enter an integer to be summed");printf("(q to quit):");status = scanf("%ld", &num);while (status == 1) /* == 的意思是 “等于” */{sum = sum + num;printf("please enter next integer (q to quit):");status = scanf("%ld", &num);}printf("Those integers sum to %ld.\n", sum);}
程序运行结果
Please enter an integer to be summed(q to quit):21
please enter next integer (q to quit):22
please enter next integer (q to quit):23
please enter next integer (q to quit):w
Those integers sum to 66.
==
运算符是C的相等运算符(equality operator).
while语句
while
循环的通用形式如下:
while (expression)statement
statement部分可以是以分号结尾的简单语句,也可以是用花括号括起来的复合语句。
如果expression为真(或者更一般地说,非零),执行statement部分一次,然后再判断expression。
终止循环
/* 何时退出循环 */#include "stdio.h"int main(void)
{int n = 5;while (n < 7){printf("n = %d\n", n);n++;printf("Now n = %d\n", n);}printf("The loop has finished.\n");return 0;
}
程序运行结果
n = 5
Now n = 6
n = 6
Now n = 7
The loop has finished.
使用while时,要牢记一点:只有在测试条件后面的单独语句(简单语句或复合语句)才是循环部分。
/* 注意分号地位置 */#include "stdio.h"int main(void)
{int n = 0;while (n++ < 3); printf("n is %d\n", n);printf("That's all this program does.\n");return 0;
}
程序运行结果
n is 4
That's all this program does.
在该例中,测试条件后面地单独分号是空语句(null statement)
关系运算符和表达式比较大小
关系表达式
:relational expression
关系运算符
:relational operator
/* 浮点数比较 */#include "math.h"
#include "stdio.h"int main(void)
{const double ANSWER = 3.14159;double response;printf("What is the value of pi?\n");scanf("%lf", &response);while(fabs(response - ANSWER) > 0.0001){printf("Try again!\n");scanf("%lf", &response);}printf("Close enough!\n");return 0;
}
程序运行结果
What is the value of pi?
3.14
Try again!
3.1415926
Close enough!
上述程序一直会提示用户继续输入,除非用户输入的值与正确值之间相差小于0.0001
.
什么是真
/* C语言中的真和假的值 */
#include "stdio.h"int main(void)
{int true_val, false_val;true_val = (10 > 2); /* 关系为真 */false_val = (10 == 2); /* 关系为假 */printf("true = %d, false = %d\n", true_val, false_val);
}
程序运行结果
true = 1, false = 0
对C语言,表达式为真的值是1,表达式为假的值是0
其他真值
/* 那些值为真 */#include "stdio.h"int main(void)
{int n = 3;while (n){printf("%2d is true\n", n--);}printf("%2d is false\n", n);n = -3;while (n){printf("%2d is true\n", n++); }printf("%2d is false\n", n);return 0;
}
程序运行结果
3 is true2 is true1 is true0 is false
-3 is true
-2 is true
-1 is true0 is false
也可以说,只要测试条件为非零,就会执行while循环。
新的_Bool类型
在C语言中,一直用int
类型的变量表示真/假值
,C99
专门针对这种类型的变量新增了_Bool
类型,在编程中,表示真或假的变量被称为布尔变量(Boolean variable)
,所以_Bool
是C语言中布尔变量的类型名。_Bool
类型的变量只能存储1(真)
或0(假)
.
/* 使用_Bool类型的变量 variable */
#include "stdio.h"int main(void)
{long num;long sum = 0;_Bool input_is_good;printf("请输入一个整数求和");printf("(q to quit): ");input_is_good = (scanf("%ld", &num) == 1);while (input_is_good){sum = sum + num;printf("Please enter next integer (q to quit): ");input_is_good = (scanf("%ld", &num) == 1);}printf("Those integers sum to %ld.\n", sum);return 0;
}
程序运行结果
请输入一个整数求和(q to quit): 22
Please enter next integer (q to quit): 23
Please enter next integer (q to quit): 24
Please enter next integer (q to quit): q
Those integers sum to 69.
优先级和关系运算符
关系运算符的优先级比算数运算符(包括+和-)低,比赋值运算符高,例如
x > y + 2
相当于x > (y + 2)
;
x = y > 2
相当于x = (y > 2)
.
关系运算符比赋值运算符的优先级高,例如
x_bigger = x > y
相当于x_bigger = ( x > y)
.
关系运算符之间有两种不同的优先级:
高优先级组
:<<=
、>>=
低优先级组
:==
、!=
与其他大多数运算符一样,关系运算符的结合律也是从左往右。
下图为部分运算符优先级表
小结:while语句
小结:关系运算符和表达式
不确定循环和计数循环
一些while循环是不确定循环(indefinite loop);
还有另一类是计数循环(counting loop).
/* 一个计数循环 */#include "stdio.h"int main(void)
{const int NUMBER = 22;int count = 1; /* 初始化 */while (count <= NUMBER){printf("Be my Valentine!\n");count++;}return 0;
}
程序运行结果
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!
Be my Valentine!