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!