Presentation is loading. Please wait.

Presentation is loading. Please wait.

Herbert G. Mayer, PSU CS Status 7/19/2015

Similar presentations


Presentation on theme: "Herbert G. Mayer, PSU CS Status 7/19/2015"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 17 Relational and Logical Operators
Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus Expressions Relational Operators Logical Operators Examples

3 Expressions An expression is any combination of:
Constants and variables parentheses function calls operators that, when evaluated, produces a value An expression can be tested to make one selection out of several possible choices expr choice 1 choice 2

4 Table 1: C Operator Precedence Table in Decreasing Order
Description Operator Associativity 1 Function call ( ) left-to-right 5 Bitwise left shift << Array subscript [ ] Bitwise right shift >> Struct member . 6 Less than < Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == Reference (addr) & Not equal to != Unary plus + 8 Bitwise AND Unary minus - 9 Bitwise XOR ^ Logical negation ! 10 Bitwise OR | Bitwise NOT ~ 11 Logical AND && Increment (pre) ++expr 12 Logical OR || Decrement (pre) --expr 13 Conditional ? : Cast ( type ) 14 Assignment = += -= *= /= %= >>= <<= &= ^= |= Size in bytes sizeof 3 Multiplication 15 Comma , Division / Highest precedence is Level 1. Lowest precedence is Level 15. Modulo % 4 Addition Subtraction 3

5 Relational Operators Relational operators compare one value against another value, and yield boolean result Operator Description > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal to != Not equal to 4

6 the expression value is
Important: == (equal to) is not the same as = (assignment) The value of a relational expression is true or false Be careful when comparing: Numeric values of differing data types Calculated floating point numbers if expr evaluates to the expression value is true 1 false 5

7  Expr Initial values Evaluates to Value 5 >= 1 - 'A' > 'B'
m < 3 m ← 2 m ← 5 sin(x) > w+1 x ← 0, w ← 0 x ← 0, w ← -2 k == 100 k ← 75 k ← 100 k != 100 1 < u <= 3 u ← 5 true 1 false true 1 false false true 1 false true 1 true 1 false true 1

8 Logical operators are used to perform Boolean logic evaluations
A value of zero is interpreted as false A non-zero value is interpreted as true Operator Description && Logical AND || Logical OR ! Unary negation 7

9 Suppose A and B are general expressions:
C uses short-circuit evaluation; not all languages do! Short-circuit evaluation  Evaluation stops as soon as the true or false status is determined That means, some subexpressions may not get evaluated during run time A !A false 1 true A B A && B A || B false true 1 8

10 Example: Expr Initial values Evaluates to Value (5 >= 1) && 0 -
'A'<'B' && 'B'<'C' (k<1&&w==2)||n!=3 k← -2, w←2, n←3 m >= 0 && m <= 5 m ← -2 m ← 2 m ← 6 m < 0 || m > 5 false true 1 true 1 false true 1 false true 1 false true 1 9

11 Right: -1 <= x && x <= 7 Better: ( -1 <= x ) && ( x <= 7 )
Example: Write an expression to check if a variable x is between -1 and 7, inclusive. Wrong: -1 <= x <= 7 Right: -1 <= x && x <= 7 Better: ( -1 <= x ) && ( x <= 7 ) -- the “Golden Rule”: When in -- doubt: Parenthesize!! 10

12 Example: Expr Initial values Evaluates to Value sum sum ← 0 sum ← 1
false true 1 true 645 true 1 false false 11

13 Relational and logical expressions can be assigned Example:
int x = -3; bool state = ( x>5 ) || ( x<0 ); // true int a, b=0, c=3; a = (b==(c==(c++)));//( c==(c++) ) 1 // ( b==1 )  0 // a=0  0 12

14 C does not directly support set membership:
Inclusion or exclusion tests are done individually, or, if the set is ordered, by using a range test Example: Check if c is one of these characters: 'A', 'B', 'C', or 'D' c=='A' || c=='B' || c=='C' || c=='D' Alternative: c >= 'A' && c <= 'D’ Better: ( c >= 'A' ) && ( c <= 'D’ ) Check if x is specifically one of these values: 5, 10, 120 x == 5 || x == 10 || x == 120 Why would ( x >= 5 ) && ( x <= 120 ) not be correct? 13


Download ppt "Herbert G. Mayer, PSU CS Status 7/19/2015"

Similar presentations


Ads by Google