1 / 27

Selection

Selection. Flow Chart If selection If/else selection Compound statement Switch. 3.1 Flow Chart. Graphical representation of an algorithm Special-purpose symbols connected by arrows (flowlines) Rectangle symbol (action symbol: Any type of action) Oval symbol

polly
Télécharger la présentation

Selection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Selection

  2. Flow Chart • If selection • If/else selection • Compound statement • Switch

  3. 3.1Flow Chart • Graphical representation of an algorithm • Special-purpose symbols connected by arrows (flowlines) • Rectangle symbol (action symbol: Any type of action) • Oval symbol • Beginning or end of a program, or a section of code (circles) • Others see in http://deming.eng.clemson.edu/pub/tutorials/qctools/flowm.htm

  4. 3.2 if Selection Structure • Let the computer do this: • See if the weather is good. • If the weather is good, have a rest and go out (print out “go out!”) • Otherwise, stay inside and do homework • (print out “do homework.”)

  5. 3.2 if Selection Structure • Consider several things: • How to let the computer know the weather information? • What format of that information? • Selection? • Alternative actions • Go out • Do HW cin >> weather; string weather; If statement // new

  6. 3.2 if Selection Structure • Choose among alternative courses of action • example: • If (grade >= 60) { • cout <<“Passed”<<endl; • } • else { • cout <<“Failed”<<endl; • } • cout<<“end of if statement”; • If the condition is true • Print statement cout <<“Passed”<<endl; executed, program continues to the print statement cout<<“end of if statement”; • If the condition is false • Print statement cout <<“Failed”<<endl; executed, program continues (cout<<“end of if statement”;)

  7. 3.2 if Selection Structure #include <iostream> #include <string> using namespace std; int main() { string weather; cout <<"Report the current weather (good/rainning): "; cin >>weather; if(weather == "good"){ cout<<"go out "; } else { cout<<“do homework "; } return 0; }

  8. 3.2 if Selection Structure

  9. 3.2 if Selection Structure • Repeat the procedure: • How to let the computer know the weather information? • What format of that information? • Selection? • Alternative actions • Go out • Do HW Ifstream fp (“weather.dat”); fp >> weather; string weather; If statement // new

  10. 3.2 if Selection Structure #include <fstream> #include <iostream> #include <string> using namespace std; int main() { string weather; ifstream fp("weather.dat"); fp >> weather; if(weather == "good"){ cout<<"go out "; } else { cout<<"do homework "; } return 0; }

  11. 3.2 if Selection Structure • example: • If (grade >= 60) { • cout <<“Passed”; • } • If the condition is true • Print statement executed, program continues to next statement • If the condition is false • Print statement ignored, program continues

  12. 3.2 if Selection Structure #include <iostream> #include <string> using namespace std; int main() { string weather; cout <<"Report the current weather (good/rainning): "; cin >>weather; if(weather == "good"){ cout<<"go out “<<endl; } cout<<"if the weather is "<<weather<<" ->“; cout<<"Doing HW"<<endl; return 0; }

  13. A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 istrue true false cout << “Passed” grade >= 60

  14. 3.2 if Selection Structure • Single Condition • == • > • < • >= • <= • != • Combination Condition • || • && • Default Condition • Any expression, zero is false, nonzero is true. i.e., 3-4 is true.

  15. #include <fstream> #include <string> int main() { string i,filename,str; string head="repeat:"; ifstream fp; cout << "Do we need input from files (Y/N)?"<<endl; cin >> i; if(i=="y" || i=="Y"){ cout << "PLS input file name "; cin >> filename; fp.open (filename.c_str()); fp >> str; cout << head+str; } else { cout << "PLS input a string by using keyboad" <<endl; cin >> str; cout << head+str; } return 0; }

  16. 3.3 Other if Selection Structure • Simple if/else • Ternary condition operator • Nested selection

  17. 3.3.1Simple if/else • if ( grade >= 60 ){ cout << "Passed"; • }else{ cout << "Failed"; • } • If the condition is true • Print “Passed” • If the condition is false • Print “Failed”

  18. false true print “Passed” print “Failed” grade >= 60

  19. Condition Value if true Value if false false true print “Passed” print “Failed” grade >= 60 3.3.2 Ternary conditional operator (?:) • Three arguments (condition, value if true, value if false) • Code could be written: • cout << ( grade >= 60 )? “Passed” : “Failed” ;

  20. 3.3.3 Nested selection • One inside another, test for multiple cases • Once condition met, other statements skipped • if student’s grade is greater than or equal to 90 Print “A” • else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else • Print “F”

  21. Example if ( grade >= 90 ){ // 90 and above cout << "A";}else if ( grade >= 80 ){ // 80-89 cout << "B";}else if ( grade >= 70 ){ // 70-79 cout << "C"; }else if ( grade >= 60 ){ // 60-69 cout << "D";}else { // less than 60 cout << "F"; }

  22. 3.4 Compound Statement • Block • Set of statements within braces • Set of statements without a pair of braces if ( grade >= 60 ) cout << "Passed.\n"; else cout << "Failed.\n"; cout << "You must take this course again.\n"; • Without braces, cout << "You must take this course again.\n"; always executed

  23. 3.4Switch Multiple-Selection Structure • Test variable for multiple values • Series of case labels and optional default case switch ( variable ) { case value_a: // taken if variable == value1 statements break; // necessary to exit switch case value_b: case value_c: // taken if variable == value2 or == value3 statements break; default: // taken if variable matches no other cases statements break; }

  24. true false true false . . . true false case b action(s) case z action(s) case a action(s) break break break default action(s) case b case a case z

  25. #include <iostream> #include <fstream> #include <string> int main() { char i; string head="grade:"; ifstream fp; cout << "Input your selection : a, b, c, or d"<<endl; cin >> i; switch(i) { case 'a': cout << head + "10" <<endl; break; case 'b': cout << head + "20" <<endl; break; case 'c': cout << head + "30" <<endl; break; default: cout << head + "0" <<endl; } return 0; }

More Related