回圈
- 回圈
- 關系運算子:
- 賦值運算子:
- 什么是真?
- 逗號運算式:
- 下面開始介紹三種回圈:
- **1.while回圈**
- 例子:加法:
- 終止while回圈:
- 用if使while停止
- 改變運算式的值:
- 不滿足條件直接無法執行回圈的情況
- while里面的運算式可以比較字符,但是不能比較字串:
- *關系運算子也可以比較浮點數
- 關于真偽值:真為1,假為0
- *對于下面這個代碼回圈會一直進行*/
- 布爾型別:
- 2. **do while**
- 3.**for回圈**
- 利用for回圈創造一個立方表
- for的靈活性:
- 1.可以使用遞減運算子來遞減計數器
- 同樣的程式用while執行
- 用do while執行
- 2.可以讓計算器遞增2、10等
- 可以用字符代替數字計數;
- 3.除了限制測驗次數以外,也可以用test位置限制其他值
- 4.運算式
- 第三個運算式可以是任意一個合法的運算式,每次迭代都會更新該運算式的值
- 可以省略一個或者多個運算式,但是不能省略分號
- 第一個運算式也可以不是變數賦值型
- for回圈中用到陣列
- 逗號運算式:
- 4. 嵌套回圈
- 變式嵌套回圈
回圈
回圈陳述句有:while、do while 、for,
三個回圈陳述句都有一個判斷運算式,通過此運算式判斷是否執行回圈,通常滿足運算式則為真,不滿足運算式則為假,
,所以在了解回圈之前需要簡單了解一下運算式里面常用的運算子,包括關系運算子,賦值運算子,逗號運算子,
關系運算子:
| 運算子 | 含義 |
|---|---|
| < | 小于 |
| <= | 小于等于 |
| == | 等于 |
| > | 大于 |
| >= | 大于等于 |
| != | 不等于 |
賦值運算子:
a += 2 —>a = a + 2
a -= 2 —>a = a - 2
a %= 2 —>a = a % 2
a /= 2 —>a = a /2
a *= 2 —>a= a * 2
什么是真?
對于C語言來說,運算式一定有一個值,運算式為真的值是1,運算式為假的值是0
這里將在介紹while回圈處說明,
逗號運算式:
會在for回圈講到
下面開始介紹三種回圈:
注意:三個回圈都有statement部分,執行的一個單元,當執行分幾步時,可以利用括號,
1.while回圈
形式:
while(expression)
statement;屬于入口條件回圈:先判斷再執行
在expression部分為假或為0 之前,重復執行statement部分
例子:加法:
#include<stdio.h>
int main()
{
int num = 0;
int sum = 0;
int status = 0;
printf("please enter an integer to be summed\n");//提示輸入一個加數
printf("(q to quit)\n");//輸入退出
status = scanf("%d", &num);//輸入
while (status == 1)//判斷
{//while陳述句只會讀取下面一個陳述句或者代碼塊
sum = num + sum;
printf("%d", sum);
printf("please enter next integer to be summed\n");
status = scanf("%d", &num);
}//可以執行多個數相加
printf("those intergers sum to %d", sum);//列印結果
return 0;
}
終止while回圈:
要是想while終止,可以用break和if陳述句或者改變測驗運算式的值
#include<stdio.h>
int main()
{
int index = 1;
while (index < 5)
printf("good morning \n");//只有這兩行的話while回圈不會停止
return 0;
}
用if使while停止
#include<stdio.h>
int main()
{
int index = 1;
while (index < 5)
{
printf("good morning\n");
if (index < 3)
{
printf("hello\n");
break;
}
else
{
printf("no\n");
break;
}
}
return 0;
}//結果:輸出goodmorning以及hello后回圈停止
改變運算式的值:
#include<stdio.h>
int main()
{
int index = 1;
while (index < 5)
{
printf("good morning \n");
index++;
}//注意:這里需要括號將兩行代碼形成一個代碼塊,因為while只會識別下面一個陳述句
return 0;
}
#include<stdio.h>
int main()
{
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;
}//列印到 now n = 7結束,當n 等于7時,不滿足n<7的條件,不再回圈
不滿足條件直接無法執行回圈的情況
#include<stdio.h>
int mian()
{
int index = 10;
while (index++ <= 5)
printf("haha\n");
return 0;/*這個回圈不會開始,因為while是入口回圈
一開始index就不滿足 <= 5, 所以直接跳過回圈,*/
}
while里面的運算式可以比較字符,但是不能比較字串:
比如可以這樣寫:比較字符:
#include<stdio.h>
int main()
{
char ch = 'w';
int count = 1;
printf("please input char\n");
while (ch != '$')
{
count++;
scanf("%c", &ch);
printf("%d\n", count);
printf("$-->quit\n");
}
return 0;
}
*關系運算子也可以比較浮點數
盡量使用<>, 不要使用 = ,因為浮點數在系統中四舍五入可能導致邏輯上相等的兩個數不相等
浮點數比較:
#include<stdio.h>
#include<math.h>
int main()
{
const double ANSWER = 3.14159;
double reponse = 0;
printf("what is the value of pi?\n");
scanf("%lf", &reponse);
while (fabs(reponse - ANSWER) > 0.0001)
{
printf("try again!\n");
scanf("%lf", &reponse);
}
printf("close enough!\n");
return 0;
}
回圈會一直提示用戶輸入,除非與正確值之間相差值小于0.0001
關于真偽值:真為1,假為0
#include<stdio.h>
int main()
{
int mmi1 = (10 > 2);
int mmi2 = (10 < 2);
printf("true = %d\n", mmi1);//true = 1
printf("false = %d\n", mmi2);//false = 0
return 0;
}
對于下面這個代碼回圈會一直進行/
#include<stdio.h>
int main()
{
while(1)
printf("haha\n");
return 0;
}
#include<stdio.h>
int main()
{
int n = 3;//這里用整數,字串,字符都行
while (n)
{
printf("%d is true\n", n);
n--;
}//當n=0時會停止
printf("%d is false\n");//
while ('w')
{
printf("'w' is true\n");//w is true
break;
}
return 0;
}
布爾型別:
關鍵字: _Bool
布爾型別只能儲存1或者0,如果是非零其他值,會被轉換成1
#include<stdio.h>
int main()
{
_Bool index = "nihao ";//"3"可以,'w'也可以,"nihao"也可以
while (index)
{
printf("hehe\n");
break;
}
return 0;
}
2. do while
形式:
do
statement
while(expression);屬于出口條件回圈先執行一次,再判斷是否執行下一次
#include<stdio.h>
int main()
{
int sum = 0;
int count = 0;
int test = 0;
printf("please input an integer to be summed\n");
test = scanf("%d", &count);
do
{
printf("please input next integer\n");
sum = count + sum;
test = scanf("%d", &count);
}
while (test == 1);
printf(" sum = %d\n", sum);
return 0;
}
3.for回圈
形式:
for(initialize;test;update)
statement;屬于入口條件回圈
在test為假或者為0之前,重復執行statement部分
注意:入口條件回圈
#include<stdio.h>
int main()
{
const int NUMBER = 5;
int a = 0;
for (a = 1; a <= NUMBER; a++)//NUMBER限制了a加的次數
printf("haha\n");
return 0;
}

利用for回圈創造一個立方表
#include<stdio.h>
int main()
{
const int NUMBER = 6;
int count = 0;
int count2 = 0;
for (count = 1; count <= NUMBER; count++)
{
count2 = count * count * count;
printf("%d %d\n", count, count*count*count);
}//上面的count*count*count可以用count2代替
return 0;
}

for的靈活性:
1.可以使用遞減運算子來遞減計數器
#include<stdio.h>
int main()
{
const int NUMBER = 1;
int count = 0;
for (count = 7; count >= NUMBER; count--)
{
printf("%d seconds!\n", count);
}
printf("begin!");
return 0;
}

同樣的程式用while執行
:
#include<stdio.h>
int main()
{
const int NUMBER = 1;
int count = 7;
while (count >= NUMBER)
{
printf("%d seconds!\n", count);
count--;
}
printf("begin!\n");
return 0;
}

用do while執行
#include<stdio.h>;
int main()
{
const int NUMBER = 1;
int count = 7;
do
{
printf("%d seconds!\n", count);
count--;
} while (count >= NUMBER);
printf("begin!\n");
return 0;
}
結果與前面一樣
2.可以讓計算器遞增2、10等
#include<stdio.h>
int main()
{
int a = 0;
for (a = 2; a < 100; a = a + 15)
printf("%d\n", a);
return 0;
}

可以用字符代替數字計數;
#include<stdio.h>
int main()
{
char ch;
for (ch = 'a'; ch <= 'f'; ch++)
printf("the ascll value for %c is %d.\n", ch, ch);
return 0;
}//字符在內部以整數形式儲存的,
比較時,時通過轉換成整數來比較的,

3.除了限制測驗次數以外,也可以用test位置限制其他值
比如限制立方的大小
#include<stdio.h>
int main()
{
int count = 0;
for (count = 1; count * count * count <= 216; count++)
printf("%d %d\n", count, count*count*count);
return 0;
}/*注意:若在for陳述句前宣告一個變數用與test位置,
會出現錯誤--順序沒有考慮到到位
//#include<stdio.h>
//int main()
//{
// int count = 0;
// int number = 0;
// for (count = 1; number <= 216; count++)
// printf("%d %d\n", count, number);
// return 0;
//}
//*/

4.運算式
第三個運算式可以是任意一個合法的運算式,每次迭代都會更新該運算式的值
**例子1**
#include<stdio.h>
int main()
{
int x = 0;
int y = 0;
for (x = 1; y <= 75; y = (++x * 5) + 50)
//第三個運算式里面x值一直在變
printf("%10d %10d\n", x, y);
return 0;
}

例子2
每次乘上一個固定的值
#include<stdio.h>
int main()
{
double debt;
for (debt = 100.0; debt < 150; debt = debt * 1.1)
printf("your debt is now $%.2f.\n ",debt);
return 0;
}

可以省略一個或者多個運算式,但是不能省略分號
#include<stdio.h>
int main()
{
int ans = 2;
int n = 0;
for (n = 3; ans <= 25;)//省略了第三個運算式
ans = ans * n;
printf("n = %d ; ans = %d", n, ans);
return 0;
}

/#include<stdio.h>
//int main()
//{
// for (;;)/*省略了三個運算式,注意:
// 省略第二個運算式被視為真*/
// printf("haha\n");
// return 0;
//}
第一個運算式也可以不是變數賦值型
#include<stdio.h>
int main()
{
int a = 0;
int b = 1;
for (printf("haha\n"); a < b; a++)
//這里第一個運算式換成了輸出函式,一樣可以執行
printf("xixi\n");
return 0;
}
for回圈中用到陣列
#include<stdio.h>
#define NUM 10
#define MMI 72
int main()
{
int index = 0;
int sorce[NUM] = { 0 };
int sum = 0;
float ave = 0;
printf("please input your sorce\n");
for (index = 0; index <= 9; index++)
scanf("%d", &sorce[index]);//輸入是個數
for (index = 0; index <= 9; index++)
printf("%d ", sorce[index]);//檢驗那十個數
printf("\n");
for (index = 0; index <= 9; index++)
sum += sorce[index];//求和
ave = sum / NUM;//求平均數
printf("sum is %d", sum);
printf("ave is %.2f", ave);
return 0;
}
```c
逗號運算式:
這里用一個例子來解釋:
#include<stdio.h>
int main()
{
const int COUNT = 40;
const int NUMBER = 20;
int cost, ounces;
printf("ounces cost\n");
for (ounces = 1, cost = COUNT; ounces < COUNT; ounces++)
printf("%d",ounces);
return 0;
}
//這里可以同時給ounces和cost都賦值,但是有順序
//ounces比cost先賦值
//逗號運算子對于順序有要求:
//后面用不到的話不用要求順序
4. 嵌套回圈
#include<stdio.h>
int main()
{
char ch = 'a';
int b = 1;
int c = 6;
for (b = 1; b <= c; b++)//這個后面不能加分號,否則會跳過該陳述句
{
for (ch = 'a'; ch <= 'f'; ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}
變式嵌套回圈
#include<stdio.h>
int main()
{
char ch = 'a';
int b = 1;
int c = 6;
for (b = 1; b <= c; b++)
{
for (ch = ('a'+b); ch <= 'f'; ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328130.html
標籤:其他
上一篇:pyinstaller。編譯.py檔案時出現多個"未找到lib"的警告
下一篇:12-2107課上問題分析及總結
