Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang
Relational Expression * Operand Relational Operator Operand price < * Expression Examples age > 40 length <= 50 3 < 4 flag == done Selection Control Structure Dr. Ming Zhang
Invalid Relational Expression *Operator out of order length =< 50 * Invalid operator 3 >> 4 * Space are not allowed flag = = done * Assignment operator or relations operator ?? flag = done Selection Control Structure Dr. Ming Zhang
Relational Operators Operator Meaning Example < Less thanage < 30 >Greater than hight > 6.2 <=Less than/equal totaxtable<=20 >=Greater than/equal totemp>= 98.6 ==Equal to grade == 100 !=Not equal tonumber !=250 Selection Control Structure Dr. Ming Zhang
Value of Relational Operator Express Expression value Interpretation ‘A’ >’C’ 0 false ‘D’ <=‘Z’1true ‘E’==‘F’0false ‘G’ >=‘M’0 false ‘B’ !=‘C’1 true 0: false condition 1: true condition Selection Control Structure Dr. Ming Zhang
Logical Operators * Logical Operators &&: Logical operator AND ||: Logical operator OR !: Logical operator NOT * Examples - (age > 40) && (term < 10) is true only if age is greater than 40 and term is less (age > 40) || (term < 10) will be true if either age is greater than 40, term is less 10, or both condition are true. Selection Control Structure Dr. Ming Zhang
Logical Operator Expression Let: a=12.0, b=2.0, i=15, j=30, complete=0.0 Expression Value Interpretation a>b1True (i==j) || (a<b) || complete0False (a/b>5) && (i<=20)1True Selection Control Structure Dr. Ming Zhang
Precedence of Logical Operators OperatorAssociativity !Unary Right to left */%Left to right +-Left to right >=Left to right == !=Left to right &&Left to right ||Left to right = += -=*=/=Right to left Selection Control Structure Dr. Ming Zhang
Examples of Logical Operators Let: char key = ‘m’; int i = 5, j =7, k = 12; double x = 22.5 Expression Equivalent Expression Value i+2==k-1(i+2)==(k-1)? i+2*j>k(i+(2*j))>k? ‘a’+1==‘b’(‘a’+1)==‘b’? 25>+x+1.025>+(x+1.0)? Selection Control Structure Dr. Ming Zhang
Exercise: Logical Operators Let: char key = ‘A’; int i = 4, j =6, k = 9; double x = 12.5 Expression Equivalent Expression Value i+2==k-3(i+2)==(k-3)? i+2*j<k(i+(2*j))<k? ‘a’+1!=‘b’(‘a’+1)!=‘b’? 25<+x+1.025<+(x+1.0)? Selection Control Structure Dr. Ming Zhang
Numerical Accuracy Problem *Many decimal numbers, such as 0.1, for example, can not be represented exactly in binary using a finite number of bits. Thus testing for exactly equality for such numbers can be fail. * When equality of noninteger values is desired, it is better to require that the absolute value of the difference between operands be less than some extremely small number. Do not use relational operator ==. Selection Control Structure Dr. Ming Zhang
Examples of Numerical Accuracy * float operand1, operand2; operand1==operand2;/*do not use == here*/ /* using folloing condition to instead of */ fabs(operand1 - operand2) < ; * float x, y; x/y == 0.35; /* do not use == here */ /* using folloing condition to instead of */ fabs(x/y ) < ; Selection Control Structure Dr. Ming Zhang
If Statement * Syntax of if Statement if (condition) statement executed if condition is “true” * General Form of if Statement if (expression) statement; Selection Control Structure Dr. Ming Zhang
Example of If Statement * If student’s grade is greater than or equal to 60 Print “Passed” * if ( grade >= 60) cout << “passed” << endl; * grade>=60 true print “passed” false Selection Control Structure Dr. Ming Zhang
If-else Statement * Syntax of if-else Statement if (condition) statement executed if condition id “true” else statement executed if condition is “false” * General Form of if-else Statement if (expression) statement1; else statement2; Selection Control Structure Dr. Ming Zhang
Flowcharting if/else Structure false grade>= 60 true print “failed” print “passed” Selection Control Structure Dr. Ming Zhang
Exercise: Selection Structure (C++) #include using std::cout; using std::cin;using std::endl; int main( ) {int taxable; float taxes; cout << “Please type in the taxable income:”: cin >> taxable; if(taxable <=20000) taxes = 0.02*taxable; else taxes = 0.025*(taxable-20000) ; cout << “Taxes are $”<< taxes) << endl; return(0); } Selection Control Structure Dr. Ming Zhang