Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機程式 第二單元 Control Structure I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,

Similar presentations


Presentation on theme: "計算機程式 第二單元 Control Structure I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,"— Presentation transcript:

1 計算機程式 第二單元 Control Structure I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010 。 本 講義僅引用部分內容,請讀者自行準備。 1 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法 第 46 、 52 、 65 條合理使用。 Microsoft 服務合約

2 Control Structure Three control structures o Sequence structure o Selection structure o Repetition structure Selection structure o Single selection: if o Double selection: if…else o Multiple selection: nested if/else, switch Repetition structure o while o do…while o for Stacking vs. nesting 2

3 Algorithm and Pseudo-code Algorithm: o A series of action and decision o Pseudo code and flow chart o E.g., Fig.4.1 on p.112 ← pseudo code o E.g., Fig.4.2 on p.113 ← flow chart 3

4 Example of Pseudo code 4 p.112

5 Example of Flowchart 5 add grade to total add 1 to counter corresponding C++ statement total=total+grade; corresponding C++ statement counter=counter+1;

6 6 Sequence Selection if statement (single selection) [f] [t] if...else statement (double selection) [t][f] switch statement with break (multiple selection) break … default processing [t] [f] 7 Control Structures(I)

7 7 Repetition while statement [t] [f] do…while statement [t] [f] for statement body initialization increment [t] [f] 7 Control Structures(II)

8 if Selection if (condition) statement; 8 e.g., int a; cin>>a; if(a>=9) cout<<“:p”; e.g., if(a<=3) cout<<“:o”; cout<<“bye~”; e.g., if(a 9); cout<<“  ”; cout<<“:-<”; e.g., if(a<-8); cout<<“:p”; cout<<“bye~”; e.g., if(1<a<9) { cout<<“:p”; cout<<“bye~”; }

9 if/else Selection if (condition) statement1; else statement2; 9 e.g., int a; cin>>a; if(a>=2) cout<<“ ”; else cout<<“  ”; e.g., if(a<9) cout<<“ ”; cout<<“bye~”; else cout<<“  ”; cout<<“hello”; e.g., if(a>4) cout<<“:p”; else cout<<“:*p”; cout<<“bye..”; e.g., if(a=3) cout<<“:_(”; else cout<<“bye”; cout<<“zzz..”; Note: conditional operator: ? :

10 Nested if/else selection Nested if/else if (condition) statement; else if (condition) statement; else if (condition) statement; else if (condition) statement; else statement; 10 e.g., if(a==1) cout<<“Mon”; else if(a==2) cout<<“Tue”; else cout<<“bye..”; e.g., if(a>=3) if(a%2) cout =3”; else cout<<“a<3”;

11 11 stacking nesting

12 More on Operators 12 OperatorsAssociativityType ()left to rightparentheses ++ -- static_cast () left to rightunary(postfix) ++ -- + - ! right to leftunary(prefix) * / %* / % left to rightmultiplicative + -+ - left to rightadditive > left to rightinsertion/extraction >= left to rightrelational == != left to rightequality &&left to rightlogical AND ||left to rightlogical OR ?:right to leftconditional = += -= *= /= %= right to leftassignment,left to rightcomma

13 版權聲明 13 頁碼作品版權圖示來源 / 作者 1-14 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 2 Open Clip Art Library ,作者: ricardomaia ,本作品轉載自: http://openclipart.org/detail/122467/simple-gears-by-ricardomaia ,瀏覽日期: 2013/1/16 。 http://openclipart.org/detail/122467/simple-gears-by-ricardomaia 3 Open Clip Art Library ,作者: ryanlerch ,本作品轉載自: http://openclipart.org/detail/630/thinkingboy-outline-by-ryanlerch ,瀏覽日期: 2013/1/16 。 http://openclipart.org/detail/630/thinkingboy-outline-by-ryanlerch 4 Open Clip Art Library ,作者: aritztg ,本作品轉載自: http://openclipart.org/detail/3422/mouse-by-aritztg ,瀏覽日期: 2013/1/10 。 http://openclipart.org/detail/3422/mouse-by-aritztg 5 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.113 。 依據著作權法第 46 、 52 、 65 條合理使用。

14 版權聲明 14 頁碼作品版權圖示來源 / 作者 6-7 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.193 。 依據著作權法第 46 、 52 、 65 條合理使用。 11 臺灣大學電機系 廖婉君教授 12 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.147 。 依據著作權法第 46 、 52 、 65 條合理使用。


Download ppt "計算機程式 第二單元 Control Structure I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,"

Similar presentations


Ads by Google