![C程序設(shè)計英文課件:CHAPTE 3 Control Flow_第1頁](http://file4.renrendoc.com/view/0558c3e77b6119f7c7405d4e2d3be5ce/0558c3e77b6119f7c7405d4e2d3be5ce1.gif)
![C程序設(shè)計英文課件:CHAPTE 3 Control Flow_第2頁](http://file4.renrendoc.com/view/0558c3e77b6119f7c7405d4e2d3be5ce/0558c3e77b6119f7c7405d4e2d3be5ce2.gif)
![C程序設(shè)計英文課件:CHAPTE 3 Control Flow_第3頁](http://file4.renrendoc.com/view/0558c3e77b6119f7c7405d4e2d3be5ce/0558c3e77b6119f7c7405d4e2d3be5ce3.gif)
![C程序設(shè)計英文課件:CHAPTE 3 Control Flow_第4頁](http://file4.renrendoc.com/view/0558c3e77b6119f7c7405d4e2d3be5ce/0558c3e77b6119f7c7405d4e2d3be5ce4.gif)
![C程序設(shè)計英文課件:CHAPTE 3 Control Flow_第5頁](http://file4.renrendoc.com/view/0558c3e77b6119f7c7405d4e2d3be5ce/0558c3e77b6119f7c7405d4e2d3be5ce5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、CHAPTE 3statements specify the order in which computations are performedContents1. Statements and Blocks2. If-else3. Else-if4. Switch 5. Loops- While and For 6. Loops-Do-while7. Break and Continue8. Goto and Labels3.1 Statements and Blocks3.1 Statements and Blocks(語句與分程序); (Semicolon) is a statement
2、 terminator分號是語句的結(jié)束符號 (Braces) are used to group declarations and statements together into a compound statement or block. 花括號表示了復(fù)合語句或是語句組3.1 Statements and Blocksmain( ) while(c=getchar()!=EOF ) main() c=min(a,b);int min(int a, int b)if( c!=EOF) .3.2 If-Else3.2 If-ElseSyntax(語法規(guī)則):if(expression)stat
3、ement1;elsestatement2;Statement ended with ;條件判斷NOTE: else 與最近的 if 進(jìn)行組合3.2 If-Elseif(n0)if(ab)z=a;elsez=b;if (n0)if(ab)z=a;elsez=b;if(n0) if(ab)z=a; elsez=b;用 可以界定if-else的作用范圍3.2 If-Elseif(n=0) for(i=0;i0) printf(“”);else printf(“error- n is a negativen”);Note:Its a good habit to use braces when the
4、re are nested ifs.if(n=0) for(i=0;i0) printf(“”);else /*wrong */ printf(“error- n is a negativen”);3.3 Else-If3.3 Else-Ifif(expression1)statements1;else if(expression2)statements2;else if(expression3)statements3;elsestatementsn;syntaxNone of the above3.3 Else-If3.3 Else-Ifint binsearch(int x,int v ,
5、 int n)int low,high,mid;low=0;high=n-1;while(low=high) mid=(low+high)/2;if(xvmid)low=mid+1;else /* found match*/return mid;return 1;/* no match*/例:在一個數(shù)組中找與給定的值相等的數(shù)。假定數(shù)組已經(jīng)按從小到大的順序排好。3.4 Switch3.4 Switchsyntaxswitch(expression) case const-expr1: statements 1case const-expr2: statements 2.default: stat
6、ements n執(zhí)行過程:1、當(dāng)switch后面“表達(dá)式”的值,與某個case后面的“常量表達(dá)式”的值相同時,就執(zhí)行該case后面的語句(組);2、如果沒有任何一個case后面的“常量表達(dá)式”的值,與“表達(dá)式”的值匹配,則執(zhí)行default 后面的語句(組)。然后,再執(zhí)行switch語句的下一條。3.4 Switchswitch(grade) case A : printf(“85100n”); case B : printf(“7084n”); case C : printf(“6069n”); case D : printf(“60n”);defaut : printf(“errorn”)
7、;3.4 Switch說明(1)switch后面的“表達(dá)式”,可以是int、char和枚舉型中的一種。(2)每個case后面“常量表達(dá)式”的值,必須各不相同,否則會出現(xiàn)相互矛盾的現(xiàn)象(即對表達(dá)式的同一值,有兩種或兩種以上的執(zhí)行方案)。(3)case后面的常量表達(dá)式僅起語句標(biāo)號作用,并不進(jìn)行條件判斷。系統(tǒng)一旦找到入口標(biāo)號,就從此標(biāo)號開始執(zhí)行,不再進(jìn)行標(biāo)號判斷,所以必須加上break語句,以便結(jié)束switch語句。3.4 Switchswitch(grade) case A : printf(“85100n”); case B : printf(“7084n”); case C : printf(
8、“6069n”); case D : printf(“60n”);default : printf(“errorn”);若grade的值等于B,執(zhí)行結(jié)果會是什么?7084606960errorbreak可以中止switch的繼續(xù)執(zhí)行switch(grade) case A : printf(“85100n”); break; case B : printf(“7084n”); break; case C : printf(“6069n”); break; case D : printf(“60n”); break;default : printf(“errorn”);Example:count
9、 digit,white space, othersmain( )int c,i,nwhite,nother,ndigit10;nwhite=nother=0;for(i=0;i10;i+) ndigiti=0;while(c=getchar()!=EOF) switch(c) case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:ndigitc-0+;break; case : case n: case t: nwhite+; break;default:nother+; break;br
10、eak statement causes an immediate exit from swicth注:各case及default子句的先后次序,不影響程序執(zhí)行結(jié)果。多個case子句,可共用同一語句(組)。3.4 SwitchExample:Write a program that inputs arithmetic operator and variables, printing the result.main() int a,b; char op; scanf(%d%c%d,&a,&op,&b); switch(op) case +: printf(%dn“,a+b);break; cas
11、e -: printf(%dn“,a-b);break; case *: printf(%dn“,a*b);break; case /: if(b=0) printf(“division by zeron”); else printf(%dn“,a/b); break; default: printf(“unknown operatorn); 3.4 SwitchExample:Write a program that inputs arithmetic operator and variables, printing the result.main() int a,b; char op; s
12、canf(%d%c%d,&a,&op,&b); if(op=+) printf(%dn“,a+b); else if(op=-) printf(%dn“,a-b); else if(op=*) printf(%dn“,a*b); else ifop=/) if(b=0) printf(“division by zeron”); else printf(%dn“,a/b); else printf(“unknown operatorn);3.5 Loops-While and For3.5 Loops-While and Forsyntaxwhile(expression) statement;
13、for(expr1;expr2;expr3)statement;expr1;while(expr2) statement; expr3;3.5 Loops-While and Forfor(expr1;expr2;expr3)statement; expr1 and expr3 are assignment or function calls, expr2 is a relational expression . all of them can be omitted, if expr2 omitted, taken as permanently true.for(變量賦初值;循環(huán)繼續(xù)條件;循環(huán)
14、變量增值) 循環(huán)體語句組;3.5 Loops-While and Forthe expr1 is omittedfor( ;i=100; i+ )sum+=i;the expr2 is omittedfor(i=1 ; ; i+ )sum+=i;the expr3 is omittedfor(i=1;i=100; ) sum+=i; i+; the expr1 and expr3 are omitted, equal to whilefor( ;i=100;) sum+=i;i+; all are omittedfor ( ; ;) while(1)the expr1 can initiali
15、ze other expressionsfor( sum=0;i=100; i+ )sum+=i;while(i=100) sum+=i; i+; 3.5 Loops-While and Forthe expr1 and expr3 may be a Comma expression;for(sum=0,i=1; i=100; i+) sum+=i; for(i=0,j=100 ; ij; i+,j-)k=i+j; the expr2 is relational expression, 0 or not 0for ( ; (c=getchar()!=n ;)3.5 Loops-While an
16、d ForExample:convert s to integer#include int atoi(char s) int i,n,sign;for(i=0;isspace(si);i+);sign=(si=-)? 1:1;if(si=+ | si=-) i+;for(n=0;isdigit(si);i+)n=10*n+(si-0);return sign*n;3.5 Loops-While and ForExample: sort an array into increasing ordervoid popsort(int v ,int n) /*起泡法*/ int i,j,t; for(
17、j=0;jn-1;j+) for(i=0;ivi+1) t=vi; vi=vi+1; vi+1=t; 相鄰兩數(shù)相比,大數(shù)后移;大數(shù)沉底,小數(shù)浮起3.5 Loops-While and ForExample:reverse string s in place#include void reverse(char s)int c,i,j;for(i=0, j=strlen(s)-1; ij; i+, j-) c=s j ;s j =si;si=c;3.5 Loops-While and For3.6LoopsDo-While syntaxdostatementwhile (expression);N
18、ote: while and for loops test the termination condition at the top, Do-while tests at the bottom. the body is always executed at least once.3.6 LoopsDo-WhileExample:convert n to charaters in s(version1)void itoa(int n, chars)int i,sign;if(sign=n)0);if (sign=0;n-)if(sn!= &sn!=t&sn!=n)break;sn+1=0;ret
19、uen n;3.7 Break and ContinueContinue causes the next iteration of the enclosing loop(while, for, do-while) to begin. only used in loop, not in switch. 終止當(dāng)前循環(huán)運算,轉(zhuǎn)入下一次循環(huán),不能用于switch語句。3.7 Break and Continuefor(i=0,in;i+)if(ai0) /*skip negative elements*/continue;./* do positive elements*/3.7 Break and Continuewhile(expr1) . if(expr2) b
溫馨提示
- 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年度建筑安全施工材料質(zhì)量檢測合同
- 2025年度體育用品批發(fā)采購合同體育
- 2025年度生態(tài)毛竹綠色采購合同示范文本
- 2025年度專業(yè)賽車隊雇傭駕駛員及教練團(tuán)隊合同
- 綿陽2025上半年四川綿陽安州區(qū)面向區(qū)內(nèi)考調(diào)機關(guān)事業(yè)單位工作人員30人筆試歷年參考題庫附帶答案詳解
- 紹興浙江紹興市外服派駐越城機關(guān)單位景點講解員招聘筆試歷年參考題庫附帶答案詳解
- 醫(yī)用氧氣項目融資計劃書
- 深圳廣東深圳市南山區(qū)教育系統(tǒng)招聘財務(wù)人員(勞務(wù)派遣)7人筆試歷年參考題庫附帶答案詳解
- 柳州廣西柳州市第六中學(xué)參加廣西2025屆綜合性高校畢業(yè)生就業(yè)雙選會招聘教師3人筆試歷年參考題庫附帶答案詳解
- 杭州浙江杭州市生態(tài)環(huán)境局桐廬分局招聘編外工作人員筆試歷年參考題庫附帶答案詳解
- 2024版金礦居間合同協(xié)議書
- 2025內(nèi)蒙古匯能煤化工限公司招聘300人高頻重點提升(共500題)附帶答案詳解
- 小學(xué)英語 國際音標(biāo) 練習(xí)及答案
- 優(yōu)秀班主任經(jīng)驗交流課件-班主任經(jīng)驗交流課件
- HP-DL380-Gen10-服務(wù)器用戶手冊
- 2023年廣州金融控股集團(tuán)有限公司招聘筆試題庫及答案解析
- YB∕T 105-2014 冶金石灰物理檢驗方法
- 血液科品管圈匯報-PPT課件
- 騙提個人住房公積金檢討書
- 管道保溫及面積計算公式
- 江西省日照小時數(shù)
評論
0/150
提交評論