Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection. Flow Chart If selection If/else selection Compound statement Switch.

Similar presentations


Presentation on theme: "Selection. Flow Chart If selection If/else selection Compound statement Switch."— 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 http://deming.eng.clemson.edu/pub/tutorials/qctools/flowm.htm

4 3.2if 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.2if 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.2if 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.2if Selection Structure #include 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.2if Selection Structure

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

10 3.2if Selection Structure #include 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

12 3.2if 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

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

14

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

16 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.

17 #include 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; }

18 3.3 Other if Selection Structure Simple if/else Ternary condition operator Nested selection

19 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”

20 truefalse print “Failed”print “Passed” grade >= 60

21 Three arguments (condition, value if true, value if false ) Code could be written: cout = 60 )? “Passed” : “Failed” ; truefalse print “Failed”print “Passed” grade >= 60 ConditionValue if trueValue if false 3.3.2 Ternary conditional operator ( ?: )

22 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” 3.3.3 Nested selection

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

24 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.4 Compound Statement

25  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; } 3.4 Switch Multiple-Selection Structure

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

27 #include 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; }


Download ppt "Selection. Flow Chart If selection If/else selection Compound statement Switch."

Similar presentations


Ads by Google