270 likes | 472 Views
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
E N D
Flow Chart • If selection • If/else selection • Compound statement • Switch
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
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.”)
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
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”;)
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; }
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
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; }
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
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; }
A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 istrue true false cout << “Passed” grade >= 60
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.
#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; }
3.3 Other if Selection Structure • Simple if/else • Ternary condition operator • Nested selection
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”
false true print “Passed” print “Failed” grade >= 60
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” ;
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”
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"; }
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
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; }
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
#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; }