Rational Expressions. relational operators. logical operators Rational Expressions relational operators logical operators order of precedence
Expressions any combination of variables, constants and functions that can be evaluated to yield a result typically involve operators
Expressions 5 x x + y func(12.05, a, “Yvonne”) cnt++ a = 3 + j
Relational Expressions compare operands used in decision making evaluate to 1 (true) or 0 (false)
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
Relational Expressions Operand Relational Operand operator price < 34.98 expression * * *
Relational Expressions Operand Relational Operand operator... ‘Z’ < ‘K’ expression
Values of Relational Expressions
Evaluating Expressions int i = 1, j = 2, k = 3; i < j - k b i * *
Evaluating Expressions int i = 1, j = 2, k = 3; -i + 5 * j >= k + 1 i i i i b * * * * *
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 * * *
Relational Operators Examples Valid a < 3 a > b -1.1 >= (2.2 * x + 3.3) a < b < c // syntactically correct but confusing Not Valid a =< b // out of order a < = b // space not allowed a >> b // shift expression * *
Equality Operators Equal to = = Not Equal to != Note this! * * *
Values of Equality Expressions
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) * *
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 *
Logical Operators Negation (unary) ! Logical and && Logical or || *
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 * *
Logical Operators: Examples int a = 0, b = 3, c = 1, d =4; a && !c || d b F b F b T * * *
Logical Operators: Examples int a = 0, b = 3, c = 1, d =4; a && b || !c || d b F b F b T b F * * * *
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 * * *
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
Truth Table for &&, ||, !
Operator Precedence and Associativity ! not arithmetic relational logical
Logical Expressions b i r b b b b char cv; double dv = 6.5; int iv = 4; ! (cv == ‘z’) && (abs(dv) || iv) || pow(3,2) b F b T i b T r b T b T * * * * * * *
The Empty Statement The empty statement is written as a semicolon. Example: ; // an empty statement Other statements: a = b; // an assignment statement a + b + c; // legal, no useful work done cout << a() << "\n"; // a function call
Common Errors! = = means equality = used for assignment FALSE is zero TRUE is nonzero Boolean operators give a Boolean result * *
Success comes before work only in the dictionary. End Note: Success comes before work only in the dictionary. Success comes before work only in the dictionary. Success comes before work only in the dictionary.