




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000第六章 循環(huán)控制概述C語言可實現(xiàn)循環(huán)的語句:l用goto 和 if 構(gòu)成循環(huán)lwhile 語句ldo while 語句lfor 語句goto語句及用goto構(gòu)成循環(huán)vgoto語句一般格式: goto 語句標(biāo)號; .標(biāo)號:語句; PowerPoint 2000 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000v功能:無條件轉(zhuǎn)移語句v說明:l不能用整數(shù)作標(biāo)號l只能出現(xiàn)在goto所在函數(shù)內(nèi),且唯一l只能加在可執(zhí)行語句前面l限制使用goto語句 PowerPoi
2、nt 2000 PowerPoint 2000 PowerPoint 2000例 用if 和goto語句構(gòu)成循環(huán),求1001nn/*ch5_1.c*/#include main() int i,sum=0; i=1;loop: if(i=100) sum+=i; i+; goto loop; printf(%d,sum);sum=0+1sum=1+2=3sum=3+3=6sum=6+4sum=4950+100=5050循環(huán)初值循環(huán)終值循環(huán)變量增值循環(huán)條件循環(huán)體 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 從鍵盤輸入一組數(shù)據(jù),以0結(jié)束輸入,求
3、數(shù)據(jù)和/*ch5_11.c*/#include main() int number,sum=0;read_loop: scanf(%d,&number); if(!number) goto print_sum; sum+=number; goto read_loop;print_sum: printf(The total sum is %dn,sum); PowerPoint 2000 PowerPoint 2000 PowerPoint 2000while語句v一般形式:while(表達(dá)式) 循環(huán)體語句;v執(zhí)行流程:expr循環(huán)體假(0)真(非0)while PowerPoint 2
4、000 PowerPoint 2000 PowerPoint 2000v特點:先判斷表達(dá)式,后執(zhí)行循環(huán)體v說明:l循環(huán)體有可能一次也不執(zhí)行l(wèi)循環(huán)體可為任意類型語句l下列情況,退出while循環(huán)u條件表達(dá)式不成立(為零)u循環(huán)體內(nèi)遇break,return,gotol無限循環(huán): while(1) 循環(huán)體; PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 用while循環(huán)求 1001nn/*ch5_2.c*/#include main() int i,sum=0; i=1; while(i=100) sum=sum+i; i+; printf(%d
5、,sum);循環(huán)初值循環(huán)終值循環(huán)變量增值循環(huán)條件循環(huán)體 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 顯示110的平方/*ch5_21.c*/#include main() int i=1; while(i=10) printf(%d*%d=%dn,i,i,i*i); i+; 運行結(jié)果:1*1=12*2=43*3=94*4=165*5=256*6=367*7=498*8=649*9=8110*10=100 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000dowhile語句v一般形式:do 循環(huán)體語
6、句; while(表達(dá)式);v執(zhí)行流程:do循環(huán)體expr假(0)真(非0)while PowerPoint 2000 PowerPoint 2000 PowerPoint 2000v特點:先執(zhí)行循環(huán)體,后判斷表達(dá)式v說明:l至少執(zhí)行一次循環(huán)體ldowhile可轉(zhuǎn)化成while結(jié)構(gòu)expr循環(huán)體假(0)真(非0)循環(huán)體While循環(huán) PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 用dowhile循環(huán)求 1001nn/*ch5_3.c*/#include main() int i,sum=0; i=1; do sum+=i;i+; while(
7、i=100); printf(%d,sum); PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 while和dowhile比較/*ch5_4.c*/#include main() int i,sum=0; scanf(%d,&i); do sum+=i;i+; while(i=10); printf(%d,sum);main() int i,sum=0; scanf(%d,&i); while(i=10) sum+=i;i+; printf(%d,sum); PowerPoint 2000 PowerPoint 2000 Pow
8、erPoint 2000for語句v一般形式:for(expr1 ; expr2 ; expr3) 循環(huán)體語句;v執(zhí)行流程:expr2循環(huán)體假(0)真(非0)forexpr1expr3 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000vfor語句一般應(yīng)用形式:for(循環(huán)變量賦初值;循環(huán)條件;循環(huán)變量增值)循環(huán)體語句;v說明:lfor語句中expr1, expr2 ,expr3 類型任意,都可省略,但分號;不可省l無限循環(huán): for(;)lfor語句可以轉(zhuǎn)換成while結(jié)構(gòu)expr1;while(expr2)循環(huán)體語句;expr3;例 用for循環(huán)
9、求 1001nn#include main() int i,sum=0; for(i=1;i=100;i+) sum+=i; printf(%d,sum); PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例:#include main( ) int i=0; for(i=0;i10;i+) putchar(a+i); 運行結(jié)果:abcdefghij例:#include main( ) int i=0; for(;i10;i+) putchar(a+i); 例:#include main( ) int i=0; for(;i10;) putchar
10、(a+(i+); 例:#include main( ) int i=0; for(;i10;putchar(a+i),i+) ; PowerPoint 2000 PowerPoint 2000 PowerPoint 2000循環(huán)的嵌套v三種循環(huán)可互相嵌套,層數(shù)不限v外層循環(huán)可包含兩個以上內(nèi)循環(huán),但不能相互交叉v嵌套循環(huán)的執(zhí)行流程(1) while() while() . (2) do do while( ); . while( );(3) while() do while( ); . (4) for( ; ;) do while(); while() .v嵌套循環(huán)的跳轉(zhuǎn)禁止:l從外層跳入內(nèi)層l
11、跳入同層的另一循環(huán)l向上跳轉(zhuǎn) PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 循環(huán)嵌套,輸出九九表1234567891234567892468101214161836912151821242791827364554637281.ij/*ch5_5.c*/#include main() int i,j; for(i=1;i10;i+) printf(%4d,i); printf(n-n); for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?%4dn:%4d,i*j); PowerPoint 2000 Powe
12、rPoint 2000 PowerPoint 2000i10printf假(0)真(非0)i=1j+j=1j10真(非0)假(0)i+for(i=1;i10;i+) for(j=1;j10;j+) printf(j=9)?%4dn:%4d,i*j);外循環(huán)內(nèi)循環(huán) PowerPoint 2000 PowerPoint 2000 PowerPoint 2000 輔助控制語句break語句v功能:在循環(huán)語句和switch語句中,終止并跳出循環(huán)體或開關(guān)體v說明:lbreak只能終止并跳出最近一層的結(jié)構(gòu)lbreak不能用于循環(huán)語句和switch語句之外的任何其它語句之中 PowerPoint 2000
13、PowerPoint 2000 PowerPoint 2000exprbreak;假(0)真(非0)whiledobreak;.expr假(0)真(非0)while PowerPoint 2000 PowerPoint 2000 PowerPoint 2000expr2break;.假(0)真(非0)forexpr1expr3switchexpr語句組1break;語句組2break;語句組nbreak;語句組break;.const 1const 2const ndefaultcase PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 brea
14、k舉例:輸出圓面積,面積大于100時停止#define PI 3.14159main() int r; float area; for(r=1;r100) break; printf(r=%d,area=%.2fn,r,area); PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 break舉例:小寫字母轉(zhuǎn)換成大寫字母,直至輸入非字母字符#include main() int i,j; char c; while(1) c=getchar(); if(c=a & c=z) putchar(c-a+A); else break; Powe
15、rPoint 2000 PowerPoint 2000 PowerPoint 2000continue語句v功能:結(jié)束本次循環(huán),跳過循環(huán)體中尚未執(zhí)行的語句,進(jìn)行下一次是否執(zhí)行循環(huán)體的判斷v僅用于循環(huán)語句中exprcontinue;假(0)真(非0)while真(非0)docontinue;.expr假(0)whileexpr2continue;.假(0)真(非0)forexpr1expr3 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 求輸入的十個整數(shù)中正數(shù)的個數(shù)及其平均值/*ch5_12.c*/#include main() int i,n
16、um=0,a; float sum=0; for(i=0;i10;i+) scanf(%d,&a);if(a=0) continue;num+;sum+=a; printf(%d plus integers sum :%6.0fn,num,sum); printf(Mean value:%6.2fn,sum/num); PowerPoint 2000 PowerPoint 2000 PowerPoint 2000程序舉例為止最后一項的絕對值小于的近似值,直到公式求例用61071513114t=1,pi=0,n=1.0,s=1當(dāng)|t|1e-6pi=pi+tn=n+2s=-st=s/npi
17、=pi*4輸出pi分子:1,-1,1,-1分母:1,3,5,7,. PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 求Fibonacci數(shù)列:1,1,2,3,5,8,的前40個數(shù))3()2(12) 1(1121nFFFnFnFnnnf1=1,f2=1for i=1 to 20輸出f1,f2f1=f1+f2f2=f2+f11534233159710946750255142293524578241578171855377258417711121393832040570288739088169213896104181286571964181346269922746563245986321144987676546368317811217830914930352102334155 PowerPoint 2000 PowerPoint 2000 PowerPoint 2000例 判斷m是否素數(shù)讀入mk=mi=2當(dāng)ikm被i整除
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國路由器市場十三五規(guī)劃及發(fā)展策略分析報告
- 2025-2030年中國藥用碘行業(yè)十三五規(guī)劃與發(fā)展前景分析報告
- 2025-2030年中國背投式投影電視機(jī)項目投資風(fēng)險分析報告
- 2025-2030年中國翻譯行業(yè)運行動態(tài)及投資發(fā)展前景預(yù)測報告
- 2025-2030年中國纜索起重機(jī)市場運行態(tài)勢及發(fā)展趨勢分析報告
- 2025-2030年中國硫鐵礦燒渣行業(yè)運行動態(tài)規(guī)劃研究報告
- 2025-2030年中國鹽酸美金剛行業(yè)競爭格局及發(fā)展規(guī)劃分析報告
- 2025-2030年中國白紙板市場發(fā)展趨勢與投資戰(zhàn)略研究報告
- 2025安徽省建筑安全員A證考試題庫附答案
- 出租共享菜園合同范例
- 【歷史】唐朝建立與“貞觀之治”課件-2024~2025學(xué)年統(tǒng)編版七年級歷史下冊
- 2024化工園區(qū)危險品運輸車輛停車場建設(shè)規(guī)范
- 第1課 精美絕倫的傳統(tǒng)工藝 課件 2023-2024學(xué)年贛美版初中美術(shù)八年級下冊
- 云南省地質(zhì)災(zāi)害群測群防手冊
- 五金沖壓件作業(yè)指導(dǎo)書
- 食品工業(yè)企業(yè)誠信管理體系建立及實施
- 汽車吊車吊裝施工方案
- 《植物保護(hù)學(xué)通論》PPT課件.ppt
- 倉內(nèi)運營方案
- 江蘇省電力條例(2020)
評論
0/150
提交評論