Rational Expressions relational operators logical operators order of

  • Slides: 28
Download presentation
Rational Expressions relational operators logical operators order of precedence

Rational Expressions relational operators logical operators order of precedence

Expressions © any combination of variables, constants and functions that can be evaluated to

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

Expressions 5 x x+y func(12. 05, a, “Yvonne”) cnt++ a=3+j

Relational Expressions © compare © used operands in decision making © evaluate to 1

Relational Expressions © compare © used operands in decision making © evaluate to 1 (true) or 0 (false)

Relational Operators less than < greater than > less than or equal to <=

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 price Relational Operand operator < 34. 98 expression ***

Relational Expressions Operand price Relational Operand operator < 34. 98 expression ***

Relational Expressions Operand ‘Z’ Relational Operand operator. . . < expression ‘K’

Relational Expressions Operand ‘Z’ Relational Operand operator. . . < expression ‘K’

Values of Relational Expressions

Values of Relational Expressions

Evaluating Expressions int i = 1, j = 2, k = 3; i <

Evaluating Expressions int i = 1, j = 2, k = 3; i < j - k i b **

Evaluating Expressions int i = 1, j = 2, k = 3; -i +

Evaluating Expressions int i = 1, j = 2, k = 3; -i + 5 * j >= k + 1 i i b *** **

Evaluating Expressions int i = 1, j = 2, k = 3; double x

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 +

Relational Operators Examples Valid a<3 a>b -1. 1 >= (2. 2 * x + 3. 3) a < b < c // syntactically correct but confusing a =< b a<=b a >> b Not Valid // out of order // space not allowed // shift expression **

Equality Operators Equal to == Not Equal to != Note this! ***

Equality Operators Equal to == Not Equal to != Note this! ***

Values of Equality Expressions

Values of Equality Expressions

Equality Operators Examples Valid c == 'A' k != -2 y == 2 *

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

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: |operand 1 - operand 2| < epsilon Ex. x/y == 17 abs(x/y - 17) < 0. 000001 *

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

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

Logical Operators: Examples Valid a && b a || b && c !(a <

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

Logical Operators: Examples int a = 0, b = 3, c = 1, d

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

Logical Operators: Examples int a = 0, b = 3, c = 1, d

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

Logical Operators Expression Equivalent !(a == b) !(a == b || a == c)

Logical Operators Expression Equivalent !(a == b) !(a == b || a == c) !(a == b && c > d) a != b && a != c a != b || c <= d ***

Logical Expressions: Boolean This is for version 4 only. typedef int boolean; const boolean

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 &&, ||, !

Truth Table for &&, ||, !

Operator Precedence and Associativity not arithmetic relational logical !

Operator Precedence and Associativity not arithmetic relational logical !

Logical Expressions char cv; double dv = 6. 5; int iv = 4; !

Logical Expressions char cv; double dv = 6. 5; int iv = 4; ! (cv == ‘z’) && (abs(dv) || iv) || pow(3, 2) b F r i b. T T b b. T ****

The Empty Statement The empty statement is written as a semicolon. Example: ; //

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

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

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

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