Presentation is loading. Please wait.

Presentation is loading. Please wait.

第三單元 Control Structure II

Similar presentations


Presentation on theme: "第三單元 Control Structure II"— Presentation transcript:

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

2 More on Operators

3 Switch Statement switch(){ case ’A’: statement; break; case ‘B’: default: }

4 Example Fig p.177~179

5 while and do..while loop statements
while(condition) statement; e.g., int a =1 ; while(a<=10) cout << a; a++; do{ statement;} while(conditions); e.g., int a=1; do { cout <<a; a++;} while(a<=10);

6 More on while loop int a=1,total=0; int a=1,total=0; do do { {
total+=a++; } while(a<=10); counter-controlled loop int a=1,total=0; do { cin >>a; total+=a; } while(a!=-1); Sentinel controlled loop

7 for Loop Statement for(initialization;condition;inc/dec) statement;
e.g., for(int a=1;a<=10;a++) cout << a; for(int i=1,j=10;i<=j;i++,j--) cout << i+j;

8 More Examples on for Loop
for statement examples vary control variable from 1 to 100 in increments of 1 for(int i=1;i<=100;i++) vary control variable from 100 to 1 in increments of -1 for(int i=100;i>=1;i--) vary control variable from 7 to 77 in steps of 7 for(int i=7;i<=77;i+=7) vary control variable from 20 to 2 in steps of -2 for(int i=2;i>=2;i-=2) vary control variable over the sequence 2,5,8,11,14,17,20 for(int i=2;i<=20;i+=3) vary control variable over the sequence 99,88,77,66,55,44,33,22,11,0 for(int i=99;i>=0;i-=11)

9 An Example Using for Statement
Fig. 5.6 on page 195 a = p (1+r)^n; Standard library function std::pow Calculates an exponent Example pow(x,y) Calculates the value of x raised to the yth power Requires header file <cmath>

10 More Examples in C++ Standard Lib

11 More Examples in C++ Standard Lib

12 Example Fig.5.6 p.172

13 break statement Fig p.185

14 continue statement Fig p.186

15 版權聲明 頁碼 作品 版權圖示 來源/作者 1-15 本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權法第46、52、65條合理使用。 2 C++ How to Program, 7/e,作者:Harvey M. Deitel and Paul J. Deitel, 出版社:Deitel & Associates,出版日期:2010,P.191。 依據著作權法第46、52、65條合理使用。 10-11 出版社:Deitel & Associates,出版日期:2010,P.211。 4、12-14 Open Clip Art Library,作者:aritztg,本作品轉載自:


Download ppt "第三單元 Control Structure II"

Similar presentations


Ads by Google