《循環(huán)語句》PPT課件.ppt

上傳人:za****8 文檔編號:20690983 上傳時(shí)間:2021-04-14 格式:PPT 頁數(shù):45 大?。?42.02KB
收藏 版權(quán)申訴 舉報(bào) 下載
《循環(huán)語句》PPT課件.ppt_第1頁
第1頁 / 共45頁
《循環(huán)語句》PPT課件.ppt_第2頁
第2頁 / 共45頁
《循環(huán)語句》PPT課件.ppt_第3頁
第3頁 / 共45頁

下載文檔到電腦,查找使用更方便

9.9 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《《循環(huán)語句》PPT課件.ppt》由會(huì)員分享,可在線閱讀,更多相關(guān)《《循環(huán)語句》PPT課件.ppt(45頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。

1、第 4 章 循環(huán) C+程序設(shè)計(jì) 2 主要內(nèi)容 while 循環(huán) do-while 循環(huán) for 循環(huán) 循環(huán)的嵌套 轉(zhuǎn)向語句 實(shí)例研究 小結(jié)及作業(yè) C+程序設(shè)計(jì) 3 4.1 while 循環(huán) 循環(huán)是一種控制語句塊 重復(fù)執(zhí)行 的結(jié)構(gòu),是程序設(shè)計(jì)的基 本概念 循環(huán)可用來控制一個(gè)操作或一個(gè)操作序列連續(xù)執(zhí)行多少次 C+提供三種循環(huán)語句: while 循環(huán), do- while循環(huán)和 for 循環(huán) while 循環(huán)的語法形式 while (表達(dá)式 ) 語句; 可以是復(fù)合語句,其中必須含有改 變條件表達(dá)式值的語句 執(zhí)行順序:先判 斷表達(dá)式的值, 為 true 時(shí),再 執(zhí)行語句 C+程序設(shè)計(jì) 4 4.1 wh

2、ile 循環(huán) #include using namespace std; int main( ) int i=1,sum=0; while (i=100) sum=sum+i; i+; coutsum=sumendl; 例:求 1+2+3+ +100。用流程圖表示算法,根據(jù)流程圖寫出程序 開始 int i=1,sum=0; i=100? sum=sum+i; i+; T coutsum=sumendl; 結(jié)束 F C+程序設(shè)計(jì) 5 4.1 while 循環(huán) 對數(shù)學(xué)學(xué)習(xí)工具程序的改進(jìn) 改進(jìn)一:一次生成 10 個(gè)問題,回答完畢后統(tǒng)計(jì)回答正確題數(shù)并 給出完成整個(gè)測試所有時(shí)間 #include #in

3、clude / for time function #include / for the srand and rand functions using namespace std; int main() int correctCount = 0; / Count the number of correct answers int count = 0; / Count the number of questions long startTime = time(0); C+程序設(shè)計(jì) 6 4.1 while 循環(huán) while (count 10) / 1. Generate two random s

4、ingle-digit integers srand(time(0); int number1 = rand() % 10; int number2 = rand() % 10; / 2. If number1 number2, swap number1 with number2 if (number1 number2) int temp = number1; number1 = number2; number2 = temp; / 3. Prompt the student to answer what is number1 ?number2? cout What is number1 -

5、number2 answer; C+程序設(shè)計(jì) 7 4.1 while 循環(huán) / 4. Grade the answer and display the result if (number1 - number2 = answer) cout You are correct!n; correctCount+; else cout Your answer is wrong.n number1 - number2 should be (number1 - number2) endl; / Increase the count count+; /end of while long endTime = t

6、ime(0); long testTime = endTime - startTime; cout Correct count is correctCount nTest time is testTime secondsn; return 0; C+程序設(shè)計(jì) 8 4.1 while 循環(huán) 對數(shù)學(xué)學(xué)習(xí)工具程序的改進(jìn) 改進(jìn)二:由測試者控制是否繼續(xù)生成問題,回答完畢后統(tǒng)計(jì)回答 正確題數(shù)并給出完成整個(gè)測試所有時(shí)間 #include #include / for time function #include / for the srand and rand functions using namespa

7、ce std; int main() int correctCount = 0; / Count the number of correct answers int count = 0; / Count the number of questions long startTime = time(0); char chContinue = Y; C+程序設(shè)計(jì) 9 4.1 while 循環(huán) while (chContinue =Y) / Increase the count count+; / Prompt the user for confirmation? coutchContinue; C+

8、程序設(shè)計(jì) 10 4.2 do-while循環(huán) 是 while 循環(huán)的一種變形,其語法形式為: do 語句; while(表達(dá)式 ); 執(zhí)行順序 先執(zhí)行循環(huán)體語句,后判斷條件;表達(dá)式為 true 時(shí),繼續(xù)執(zhí)行循環(huán)體 與 while 語句的比較 while 語句先判斷表達(dá)式的值,為 true 時(shí),再執(zhí)行語句 可以是復(fù)合語句,其 中必須含有改變條件 表達(dá)式值的語句 C+程序設(shè)計(jì) 11 4.2 do-while循環(huán) 用 do-while語句求 1+2+3+100, 根據(jù)流程圖,編寫程序 #include using namespace std; int main( ) int i=1,sum=0; d

9、o sum=sum+i; i+; while (i=100); coutsum=sumendl; return 0; 開始 int i=1,sum=0; i=100? sum=sum+i; i+; T coutsum=sumendl; 結(jié)束 F C+程序設(shè)計(jì) 12 4.3 for 循環(huán) for 循環(huán)語法形式為 : for (表達(dá)式 1;表達(dá)式 2;表達(dá)式 3) 語句; C+中的 for語句使用最為廣泛和靈活,不僅 可以用于循環(huán)次數(shù)已經(jīng)確定的情況,而且可 以用于循環(huán)次數(shù)不確定而只給出循環(huán)結(jié)束條 件的情況 循環(huán)前 先求解 循環(huán)條 件判定 每次執(zhí)行完循 環(huán)體后求解 C+程序設(shè)計(jì) 13 4.3 for

10、 循環(huán) /用 for循環(huán)語句求 1+2+3+100 #include using namespace std; int main( ) int i,sum=0; for(i=1; i=100; i+) sum=sum+i; coutsum=sumendl; return 0; int i=1,sum=0; for(;i=100;i+) for(int i=1,sum=0;i=100;i+) C+程序設(shè)計(jì) 14 4.3 for 循環(huán) 有關(guān) for 循環(huán)的若干說明 表達(dá)式 1可以是設(shè)置循環(huán)變量初值的賦值表達(dá)式 for 語句的一般格式中的 “表達(dá)式 1”可以省略,此時(shí)應(yīng)在 for語句之 前給循環(huán)變量

11、賦初值 ,但 “表達(dá)式 1”后的分號不能省 表達(dá)式 2不能省略,省略即不判斷循環(huán)條件,循環(huán)無終止進(jìn)行下去; 若省略需要在循環(huán)體中有跳出循環(huán)的控制語句 表達(dá)式 2一般是關(guān)系表達(dá)式 (如 i=100)或邏輯表達(dá)式 (如 ab while( ); for( ; ; ) for( ; ; ) C+程序設(shè)計(jì) 18 4.4 嵌套的循環(huán) #include #include using namespace std; int main() cout Multiplication Tablen; cout -n; / Display the number title cout | ; for (int j = 1

12、; j = 9; j+) cout setw(3) j; cout n; / Print table body for (int i = 1; i = 9; i+) cout i | ; for (int j = 1; j = 9; j+) / Display the product and align properly cout setw(3) i * j; cout n; return 0; 使用嵌套的 for循環(huán)打印乘法表 C+程序設(shè)計(jì) 19 4.5 轉(zhuǎn)向語句 C+中的轉(zhuǎn)向語句有 break, continue, goto 三種 break 語句 主要用在 switch、 while、

13、dowhile 和 for 語句中 在 switch語句中, break 用來使流程跳出 switch語句,繼續(xù)執(zhí)行 switch 后的語句 在循環(huán)語句中, break 用來從最近的封閉循 環(huán)體內(nèi)跳出 語句 1 語句 2 C+程序設(shè)計(jì) 20 4.5 轉(zhuǎn)向語句 break 語句 for(; ;) for (; ;) if (i=1) break; a=1; /break 跳至此處 while(表達(dá)式 1) if (表達(dá)式 2) break; b =1; /break 跳至此處 C+程序設(shè)計(jì) 21 4.5 轉(zhuǎn)向語句 continue 語句 只能在循環(huán)體內(nèi)使用 continue語句使流程結(jié)束本次循環(huán)

14、,進(jìn)入 下次循環(huán),但并不結(jié)束整個(gè)循環(huán) 結(jié)束本次循環(huán),程序流程轉(zhuǎn)去執(zhí)行對條件 的判斷,如果這時(shí)循環(huán)條件為真,則開始 下一次循環(huán),否則終止循環(huán) 語句 1 語句 2 C+程序設(shè)計(jì) 22 4.5 轉(zhuǎn)向語句 continue 語句 continue 語句 和 break 語句的區(qū)別 continue 語句只結(jié)束本次循環(huán)而不是終止整個(gè)循環(huán)的執(zhí)行 break 語句結(jié)束本次循環(huán),不再進(jìn)行條件判斷 for (int n=100;n=200;n+) if (n%3 = 0) continue; coutnendl; while(表達(dá)式 1) if (表達(dá)式 2) continue; C+程序設(shè)計(jì) 23 4.5 轉(zhuǎn)

15、向語句 goto 語句 將控制從它所在的地方轉(zhuǎn)移到標(biāo)識符所標(biāo)識的語句處 大量使用 goto 語句將會(huì)使程序的流程無規(guī)律,降低程序的可讀性, 程序設(shè)計(jì)中盡量少用 goto 語句 小結(jié) 總是可以不使用轉(zhuǎn)向控制語句的循環(huán)代碼 使用轉(zhuǎn)向控制語句的目的是使代碼簡化,程序易讀 C+程序設(shè)計(jì) 24 4.6 綜合示例 求循環(huán)次數(shù) : 1) while (int i=0) i-; 2) int i=5; do couti-=0); 無限 20 C+程序設(shè)計(jì) 25 實(shí)例 1:輸入一個(gè)整數(shù) m ,判 斷它是否是素?cái)?shù) 數(shù)學(xué)定義:素?cái)?shù)是指能被 1和 它自身整除外,不能被其它任何 整數(shù)整除的數(shù)。 方法 1: 2 m-1整

16、除? 方法 2:數(shù)學(xué)證明 2 整除? 整除的 C+語言表示: X %K =0? 4.6 實(shí)例研究 m 開始 輸入整數(shù) m i=k? m%i = 0? 輸出 m不是素?cái)?shù) 結(jié)束 Y N ii+1 Y N 輸出 m是素?cái)?shù) i2, km C+程序設(shè)計(jì) 26 4.6 實(shí)例研究 #include #include using namespace std; int main() int m, i, k; coutm; i =2; k = (int)sqrt(m); while (i=k) if (m % i = 0) coutmk) coutm is a prime number!n; / return 0

17、; 開始 輸入整數(shù) m ik) 如何? C+程序設(shè)計(jì) 27 #include #include using namespace std; int main() int m, i, flag = 1; /*標(biāo)志變量 flag初值置為 1*/ coutm; for (i=2; i=m-1; i+) if (m % i = 0) flag = 0; coutiendl; if (flag) / 等價(jià)于 if (flag=1) coutNo divisor! It is a prime number.n; coutProgram is over!n; return 0; 4.6 實(shí)例研究 思考 (1)

18、這個(gè)程序是做什么的? (2)求 2 100之間的質(zhì)數(shù)并以每行 顯示 5 個(gè)質(zhì)數(shù) ,請編寫程序 C+程序設(shè)計(jì) 28 #include using namespace std; int main() int m, i, nCount = 0; bool bIsPrime; for(m = 2;m=100;m+) /bIsPrime初值為 true bIsPrime = true; for(i = 2;i=m-1;i+) if (m % i = 0) bIsPrime =false; break; if (bIsPrime) /等價(jià)于 if (bIsPrime=true) coutmt; nCoun

19、t+; if (nCount % 5 =0) coutendl; / end of for(m=1;) return 0; 4.6 實(shí)例研究 這個(gè)程序就是求 2 100之 間的質(zhì)數(shù)并以每行顯示 5 個(gè) 質(zhì)數(shù) ! C+程序設(shè)計(jì) 29 實(shí)例 2:分類統(tǒng)計(jì) 對 輸入一串字符, 統(tǒng)計(jì)其中單詞的個(gè)數(shù)、字母個(gè)數(shù)、數(shù)字個(gè)數(shù)。規(guī)定 單詞之間用一個(gè)空白符分開(空白符包括空格符、水平制表符、換行符) , 以 z表示輸入結(jié)束。 分析: ( 1)統(tǒng)計(jì)單詞的個(gè)數(shù),可通過統(tǒng)計(jì)空白符的個(gè)數(shù)得到。 ( 2)由于標(biāo)準(zhǔn)輸入流 cin輸入時(shí)會(huì)把空白符作為輸入結(jié)束符,所以應(yīng)使 用 cin.get() 函數(shù)逐一讀取字符。 4.6 實(shí)例

20、研究 C+程序設(shè)計(jì) 30 #include using namespace std; int main() int alpha =0, num=0,ch=0,word=0; char c; cout=a else ch+; / end of while coutword=word talpha=alpha tnum=num tch=ch0, n為整數(shù) ! 1 2 3nn 開始 輸入 n int i,n,p; i1;p1; i=n? pp*i;ii+1; 打印 n, p 結(jié)束 #include using namespace std; int main() int i,n,p; coutn; f

21、or(i=1,p=1;i=n;i+) p = p*i; coutn! = pendl; return 0; C+程序設(shè)計(jì) 32 4.6 實(shí)例研究 實(shí)例 4: 用下面公式求 的近似值: 直到最后一項(xiàng)的絕對值小于 10-7為止 #include #include #include using namespace std; int main( ) int s=1; double n=1,t=1,pi=0; while( fabs(t) =1e-7 ) pi = pi + t; n = n + 2; s = -s; t = s/n; pi = pi*4; coutpi= setiosflags(ios

22、:fixed) setprecision(6) piendl; return 0; 1 1 11 4 3 5 7 C+程序設(shè)計(jì) 33 實(shí)例 5: 計(jì)算 sin(x) 的值 , 公式為: 當(dāng)?shù)?n 項(xiàng)的絕對值小于 10-5 時(shí)結(jié)束 。 方法一:利用學(xué)過的階乘計(jì)算程序,逐項(xiàng)求值,然后再求和 4.6 實(shí)例研究 C+程序設(shè)計(jì) 34 4.6 實(shí)例研究 #include # include using namespace std; int main() double const fPI = 3.1415926; int i,n,p,s; double fX,fX1; double fSum,fItem;

23、coutfX; fX1 = (fX/180.0)*fPI; /弧度 n = 1; s = 1; fSum = 0; do / 求 n 的階乘 for (i=1,p=1;i= 1e-5 ); coutsin(fX) = fSum=1e-5 ) fSun += fItem; fItem = -fItem *fX1*fX1/(n+1)*(n+2); n = n+2; 2 21( 1 ) , , 1 , 3 , 5( 1 ) 2nn xt t t x n nn C+程序設(shè)計(jì) 36 實(shí)例 6:百元買百雞問題 ( 窮舉法 ) 假定小雞每只 5角 , 公雞每只 2元 , 母雞每只 3元 。 現(xiàn)有 100元錢

24、要買 100只雞 , 列出所有可能的購雞方案 。 分析: (1) 設(shè)母雞 、 公雞 、 小雞各為 x、 y、 z只 , 列出方程為: x+y+z = 100 3x+2y+0.5z = 100 三個(gè)未知數(shù) , 兩個(gè)方程 , 此題有若干個(gè)整數(shù)解 。 (2) 采用試湊法 (也稱為窮舉法或枚舉法 )來實(shí)現(xiàn) , 即將可能出現(xiàn)的各種 情況一一羅列測試 , 判斷是否滿足條件 , 采用循環(huán)結(jié)構(gòu)來實(shí)現(xiàn) 。 4.6 實(shí)例研究 C+程序設(shè)計(jì) 37 用二重循環(huán)來實(shí)現(xiàn) : for(x=0;x=33;x+) for(y=0;y=50;y+) z=100-x-y; if(3*x+2*y+0.5*z)=100) coutse

25、tw(5)xsetw(5)ysetw(5)zendl; 用三重循環(huán)來實(shí)現(xiàn) : for(x=0;x100;x+) for(y=0;y100;y+) for(z=0;z100;z+) if(3*x+2*y+0.5*z)=100) 4.6 實(shí)例研究 能否有更少的計(jì) 算來解該題? C+程序設(shè)計(jì) 38 4.6 實(shí)例研究 * 1 2 3 4 5 6 7 8 9 - 1 1 2 2 4 3 3 6 9 4 4 8 12 16 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 64 9 9 18 27

26、36 45 54 63 72 81 * 1 2 3 4 5 6 7 8 9 - 1 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 9 12 15 18 21 24 27 4 16 20 24 28 32 36 5 25 30 35 40 45 6 36 42 48 54 7 49 56 63 8 64 72 9 81 實(shí)例 7:乘法口訣表 C+程序設(shè)計(jì) 39 4.6 實(shí)例研究 乘法表分析 : 組成:表頭、表體兩部分 格式:每項(xiàng)至少需要 3個(gè)字符寬度 實(shí)現(xiàn): 表頭:直接用 cout 輸出即可 表體:需要二重循環(huán);左下三角為空格,右上三角為輸出項(xiàng),分隔線為對

27、 角線(行、列相等);左下三角為空格的計(jì)算 /表頭 coutsetw(3) *; for (i=1;i=9;i+) coutsetw(3) i; coutendl; for (j=1;j=10;i+) cout- - -; coutendl C+程序設(shè)計(jì) 40 4.6 實(shí)例研究 * 1 2 3 4 5 6 7 8 9 - 1 1 2 2 4 3 3 6 9 4 4 8 12 16 5 5 10 15 20 25 6 6 12 18 24 30 36 7 7 14 21 28 35 42 49 8 8 16 24 32 40 48 56 64 9 9 18 27 36 45 54 63 72 8

28、1 for (i=1;i=9;i+) coutsetw(3)i; for (j=1;j=j) coutsetw(3)i*j; coutendl; C+程序設(shè)計(jì) 41 4.6 實(shí)例研究 * 1 2 3 4 5 6 7 8 9 - 1 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 9 12 15 18 21 24 27 4 16 20 24 28 32 36 5 25 30 35 40 45 6 36 42 48 54 7 49 56 63 8 64 72 9 81 for (i=1;i=9;i+) coutsetw(3)i; for (j=1;jj) co

29、utsetw(3) ; else coutsetw(3)i*j; coutendl; C+程序設(shè)計(jì) 42 #include #include using namespace std; int main() int i,j; coutt 九九乘法表 endl; coutsetw(3)*; for (i=1;i=9;i+) coutsetw(3)i; coutendl; for (i=1;i=10;i+) cout-; coutendl; for (i=1;i=9;i+) coutsetw(3)i; for (j=1;j=j) coutsetw(3)i*j; coutn (2) m 除以 n 得余

30、數(shù) r (3) 若 r為 0 則 n 為最大公約數(shù),結(jié)束;否則執(zhí)行 (4) (4) m n, n r,再重復(fù)執(zhí)行 (2) m n r 12 5 2 5 2 1 2 1 0 4.6 實(shí)例研究 輾轉(zhuǎn)相除法 while (r=m % n)!=0) m = n; n = r; coutn n=n-m nm m、 n為公約數(shù) m=n while (m!=n) if(mn) m = m-n; else n = n m; coutmendl; C+程序設(shè)計(jì) 44 /輾轉(zhuǎn)相減法 #include using namespace std; int main( ) int m, n, t; cout請輸入 m,n

31、:mn; while (m!=n) if (mn) m = m -n; else n= n -m; cout最大公約數(shù)為 nendl; return 0; /輾轉(zhuǎn)相除法 #include using namespace std; int main( ) int m, n, t, r; cout請輸入 m, n:mn; if (m n) t =m; m =n; n =t; while (r=m % n)!=0) m=n; n=r; cout最大公約數(shù)為 nendl; return 0; 4.6 實(shí)例研究 C+程序設(shè)計(jì) 45 小結(jié)與作業(yè) 這一章我們學(xué)習(xí)了。 while循環(huán) do-while循環(huán) for 循環(huán) 轉(zhuǎn)向控制語句 常用算法:求和,求素?cái)?shù),求階乘,求 sin(x),乘法表 作業(yè) 教材 P98: 習(xí)題 4.8;綜合題 4.14,4.15; 程序設(shè)計(jì)練習(xí) 4.2,4.6,4.19 預(yù)習(xí)實(shí)驗(yàn)指導(dǎo)書實(shí)驗(yàn)四

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!