Control Structure Three control structures o Sequence structure

  • Slides: 14
Download presentation

Control Structure • Three control structures o Sequence structure o Selection structure o Repetition

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

Algorithm and Pseudo-code • Algorithm: o A series of action and decision o Pseudo

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

Example of Pseudo code p. 112 4

Example of Pseudo code p. 112 4

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

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

Selection Sequence switch statement with break (multiple selection) if statement (single selection) [f ]

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

Repetition while statement do…while statement for statement initialization [f ] [t ] [f ]

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

if Selection • if (condition) statement; e. g. , int a; cin>>a; if(a>=9) cout<<“:

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

if/else Selection • if (condition) statement 1; else statement 2; e. g. , int

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

Nested if/else selection • Nested if/else if (condition) statement; else statement; e. g. ,

Nested if/else selection • Nested if/else if (condition) statement; else statement; 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<<“a>=3”; else cout<<“a<3”; 10

stacking nesting 11

stacking nesting 11

More on Operators () ++ -- static_cast<type>() ++ -- + - ! * /

More on Operators () ++ -- static_cast<type>() ++ -- + - ! * / % + << >> < <= > >= == != && || ? : = += -= *= /= %= , Associativity left to right to left to right left to right to left right to left to right Type parentheses unary(postfix) unary(prefix) multiplicative additive insertion/extraction relational equality logical AND logical OR conditional assignment comma 12

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

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