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
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
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.
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!
A Relational Expression Operand Relational Operand operator price < Expression The expression is either true or false 1 or 0 * * *
Relational Expressions Operand Relational Operand operator... ‘Z’ < ‘K’ Expression The expression is either true or false (In this case, it’s always false)
Values of Relational Expressions
Evaluating Expressions int i = 1, j = 2, k = 3; i < j - k * * i b 0 False The < operator has lower precedence than the - operator
Evaluating Expressions int i = 1, j = 2, k = 3; -i + 5 * j >= k + 1 * * * * i i i i b True
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 True
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
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!
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) < * 1/ …
Logical Operators AKA Boolean Operators Negation (unary) ! Logical and&& Logical or|| *
Boolean Logic * && AND || 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 * Evaluate && before ||
int a = 0, b = 3, c = 1, d =4; a && !c || d b F b F * * * Logical Operators: Examples b T
int a = 0, b = 3, c = 1, d =4; a && b || !c || d b F b F b F * * Logical Operators: Examples b T
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
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 relational logical arithmetic ! not
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
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
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:
End Note : Success comes before work only in the dictionary.