Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | brice-stone |
View: | 220 times |
Download: | 2 times |
If you can't read please download the document
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 < 10.63* Expression Examplesage > 40length
Invalid Relational Expression*Operator out of order length =< 50* Invalid operator3 >> 4* Space are not allowedflag = = 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= 98.6==Equal to grade == 100!=Not equal tonumber !=250 Selection Control Structure Dr. Ming Zhang
Value of Relational Operator Express Expression value InterpretationA >C 0 falseD =M0 falseB !=C1 true
0: false condition1: 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 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
Logical Operator Expression Let:a=12.0, b=2.0, i=15, j=30, complete=0.0
Expression Value Interpretationa>b1True(i==j) || (a5) && (i
Precedence of Logical OperatorsOperatorAssociativity!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 OperatorsLet:char key = m;int i = 5, j =7 , k = 12;double x = 22.5
Expression Equivalent Expression Valuei+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 OperatorsLet:char key = A;int i = 4, j =6 , k = 9;double x = 12.5
Expression Equivalent Expression Valuei+2==k-3(i+2)==(k-3)?i+2*j
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) < 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
If Statement* Syntax of if Statementif (condition)statement executed if condition is true
* General Form of if Statement if (expression)statement; Selection Control Structure Dr. Ming Zhang
If-else Statement* Syntax of if-else Statementif (condition)statement executed if condition id trueelsestatement executed if condition is false
* General Form of if-else Statement if (expression)statement1;elsestatement2; Selection Control Structure Dr. Ming Zhang
Flowcharting if/else Structure
false grade>= 60 true
print failed print passed
Selection Control Structure Dr. Ming Zhang