More on Operators 2 Switch Statement switch case

  • Slides: 15
Download presentation

More on Operators 2

More on Operators 2

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

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

Example Fig. 5. 10 p. 177~179 4

Example Fig. 5. 10 p. 177~179 4

while and do. . while loop statements • while(condition) statement; • do{ statement; }

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

More on while loop • int a=1, total=0; do { total+=a++; } while(a<=10); counter-controlled

More on while loop • int a=1, total=0; 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 6

for Loop Statement • for(initialization; condition; inc/dec) statement; e. g. , for(int a=1; a<=10;

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

More Examples on for Loop • for statement examples o vary control variable from

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

An Example Using for Statement • Fig. 5. 6 on page 195 o a

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

More Examples in C++ Standard Lib 10

More Examples in C++ Standard Lib 10

More Examples in C++ Standard Lib 11

More Examples in C++ Standard Lib 11

Example Fig. 5. 6 p. 172 12

Example Fig. 5. 6 p. 172 12

break statement Fig. 5. 13 p. 185 13

break statement Fig. 5. 13 p. 185 13

continue statement Fig. 5. 14 p. 186 14

continue statement Fig. 5. 14 p. 186 14

版權聲明 頁碼 1 -15 作品 版權圖示 來源/作者 本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權法第 46、52、65條合理使用。 2 C++ How

版權聲明 頁碼 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 C++ How to Program, 7/e,作者:Harvey M. Deitel and Paul J. Deitel, 出版社:Deitel & Associates,出版日期: 2010,P. 211。 依據著作權法第 46、52、65條合理使用。 4、12 -14 Open Clip Art Library,作者:aritztg,本作品轉載自: http: //openclipart. org/detail/3422/mouse-by-aritztg,瀏覽日期: 2013/1/10。 15