Download presentation
Presentation is loading. Please wait.
Published byLesley Nelson Modified over 6 years ago
1
Rational Expressions. relational operators. logical operators
Rational Expressions relational operators logical operators order of precedence
2
Expressions any combination of variables, constants and functions that can be evaluated to yield a result typically involve operators
3
Expressions 5 x x + y func(12.05, a, “Yvonne”) cnt++ a = 3 + j
4
Relational Expressions
compare operands used in decision making evaluate to 1 (true) or 0 (false)
5
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
6
Relational Expressions
Operand Relational Operand operator price < expression * * *
7
Relational Expressions
Operand Relational Operand operator... ‘Z’ < ‘K’ expression
8
Values of Relational Expressions
9
Evaluating Expressions
int i = 1, j = 2, k = 3; i < j - k b i * *
10
Evaluating Expressions
int i = 1, j = 2, k = 3; -i + 5 * j >= k + 1 i i i i b * * * * *
11
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 * * *
12
Relational Operators Examples
Valid a < 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 * *
13
Equality Operators Equal to = = Not Equal to != Note this! * * *
14
Values of Equality Expressions
15
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) * *
16
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) < *
17
Logical Operators Negation (unary) ! Logical and && Logical or || *
18
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 * *
19
Logical Operators: Examples
int a = 0, b = 3, c = 1, d =4; a && !c || d b F b F b T * * *
20
Logical Operators: Examples
int a = 0, b = 3, c = 1, d =4; a && b || !c || d b F b F b T b F * * * *
21
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 * * *
22
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
23
Truth Table for &&, ||, !
24
Operator Precedence and Associativity
! not arithmetic relational logical
25
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 * * * * * * *
26
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
27
Common Errors! = = means equality = used for assignment FALSE is zero
TRUE is nonzero Boolean operators give a Boolean result * *
28
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.