Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rational Expressions relational operators logical operators order of precedence.

Similar presentations


Presentation on theme: "Rational Expressions relational operators logical operators order of precedence."— Presentation transcript:

1

2 Rational Expressions relational operators logical operators order of precedence

3 Expressions © any combination of variables, constants and functions that can be evaluated to yield a result © typically involve operators

4 Sample Expressions 5 A constant is an expression x A variable is an expression x + y Binary operator func(12.05, a, “Yvonne”) cnt++ Unary operator a = 3 + j The assignment operator! B = (a = 3 + j); Functions that return a value

5 Relational Expressions compare operands used in decision making evaluate to 1 (true) or 0 (false) Another legacy or “old” C. Newer versions of C++ have a Boolean variable type.

6 Relational Operators less than< greater than> less than or equal to<= greater than or equal to>= a - b < 0 is equivalent to (a - b) < 0 Because of precedence!

7 A Relational Expression Operand Relational Operand operator price < 34.98 Expression The expression is either true or false 1 or 0 * * *

8 Relational Expressions Operand Relational Operand operator... ‘Z’ < ‘K’ Expression The expression is either true or false (In this case, it’s always false)

9 Values of Relational Expressions

10 Evaluating Expressions int i = 1, j = 2, k = 3; i < j - k * * i b 0 False The < operator has lower precedence than the - operator

11 Evaluating Expressions int i = 1, j = 2, k = 3; -i + 5 * j >= k + 1 * * * * i i i i b 10 9 4 1 True

12 Evaluating Expressions int i = 1, j = 2, k = 3; double x = 5.5, y = 7.7 x - y <= j - k + 1 (x - y) <= ((j - k) + 1) * * * i r i b Order of Evaluation  -2.2 0 1 True

13 Inequality Operators Examples Valid a b -1.1 >= (2.2 * x + 3.3) a < b < c// syntactically correct but confusing Not Valid a = > b// shift expression (more operator overloading) * Becomes 1 < c or 0 < c

14 Equality Operators Equal to == Not Equal to != * * * Note this! Equality comparisons always use == Assignment always uses = The first is algebraic equality, not the two-step assignment operation!

15 Values of Equality Expressions

16 Equality Operators Examples Valid c == 'A' k != -2 y == 2 * z - 5 Not Valid a = b // assignment statement a = = b - 1// space not allowed y =! z// this is equivalent to y = (!z) *

17 Numerical Accuracy Many decimal numbers cannot be exactly represented in binary by a finite number of bits. Thus testing for exact equality can fail. Use the technique: |operand1 - operand2| < epsilon Ex. x/y == 17 abs(x/y - 17) < 0.000001 * 1/3.333333…

18 Logical Operators AKA Boolean Operators Negation (unary) ! Logical and&& Logical or|| *

19 Boolean Logic * && AND || OR 0000 0101 1001 1111

20 Logical Operators: Examples Valid a && b a || b && c !(a < b) && c 3 && (-2 * a + 7) Not Valid a && // one operand missing a | | b // extra space not allowed a & b// this is a bitwise operation &b// the address of b * Evaluate && before ||

21 int a = 0, b = 3, c = 1, d =4; a && !c || d b F b F * * * Logical Operators: Examples b T

22 int a = 0, b = 3, c = 1, d =4; a && b || !c || d b F b F b F * * Logical Operators: Examples b T

23 Logical Operators Expression Expression Equivalent !(a == b) !(a == b || a == c) !(a == b && c > d) a != b a != b && a != c a != b || c <= d * * * There are rules for making this conversion that we will learn later

24 Logical Expressions: Boolean This is for version 4 only. typedef int boolean; const boolean TRUE = 1; const boolean FALSE = 0; ver. 5 & 6: bool is a data type

25 Truth Table for &&, ||, !

26 Operator Precedence and Associativity relational logical arithmetic ! not

27 Logical Expressions char cv = ‘a’; double dv = -6.5; int iv = 4; ! (cv == ‘z’) && (fabs(dv) || iv) || pow(3,2) i r * * * * * * * b F b T b T b T b T 96.5

28 Empty Statements & Other Oddities The empty statement is written as a lone semicolon or a pair of braces Examples: ;// an empty statement {} // Empty block statement a + b + c;// legal, no useful work done

29 Common Errors! means equality used for assignment = =means equality =used for assignment FALSE is FALSE is zero TRUE is TRUE is nonzero Boolean operators give a Boolean result * * Not understanding that:

30 End Note : Success comes before work only in the dictionary.


Download ppt "Rational Expressions relational operators logical operators order of precedence."

Similar presentations


Ads by Google