Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.

Similar presentations


Presentation on theme: "Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang."— Presentation transcript:

1 Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang

2 Relational Expression * Operand Relational Operator Operand price < 10.63 * Expression Examples age > 40 length <= 50 3 < 4 flag == done Selection Control Structure Dr. Ming Zhang

3 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

4 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

5 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

6 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 10. - (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

7 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

8 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

9 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

10 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

11 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

12 Examples of Numerical Accuracy * float operand1, operand2; operand1==operand2;/*do not use == here*/ /* using folloing condition to instead of */ fabs(operand1 - operand2) < 0.000001; * float x, y; x/y == 0.35; /* do not use == here */ /* using folloing condition to instead of */ fabs(x/y - 0.35) < 0.000001; Selection Control Structure Dr. Ming Zhang

13 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

14 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

15 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

16 Flowcharting if/else Structure false grade>= 60 true print “failed” print “passed” Selection Control Structure Dr. Ming Zhang

17 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) +400.0; cout << “Taxes are $”<< taxes) << endl; return(0); } Selection Control Structure Dr. Ming Zhang


Download ppt "Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang."

Similar presentations


Ads by Google