ITPF01 Week4 Operators and Expressions

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

ITPF 01– Object Oriented Programming 1

Operators and
Expressions
Melgine M. Bauat, MSIT
Instructor
Topic Outline

Relational and Equality Operators


Assignment Operators
Logical Operators and Expressions
Complex Logical Expressions
Increment and Decrement Operators
Relational and Equality Operators

int a = 5, b = 10;
First operand Second operand

a <= b
Relational Operator
Relational and Equality Operators

int a = 5, b = 10;
First operand Second operand

a != b
Equality Operator
Relational and Equality Operators
Relational and Equality Operators
int x,y,z; ANSWERS:
x=5; y=10; z=15;
1. x > y 1. 5>10 => false
2. y > z 2. 10>15 =>
3. (x + y) >= z false
4. z <= y + x 3. (5+10)>=15 => true
5. z != x + y 4. 15<=(10+5) => true
6. z == x + y
7. 7+z/x != y 5. 15!=(5+10) =>
8. (y+z)/x == x false
9. x+y*x != y*x+x 6. 15==(5+10) => true
10.z % x == 0 7. 7+3 != 10 => false
8. 25/5 == 5 => true
9. 5+50 !== 50+5 => false
10.0==0 => true
Relational Operators

 Warning: Do not use =<, =>, =! to compare data values


 These are invalid operators
 You will get a compiler error

a =< b a == b
b => a is equal to. The only
a =! B equality operator with
equal sign at the first
because both are equal sign
Equality Operators
 Warning: Do not use a single = for checking
equality
 This will attempt to do assignment instead
 The logical expression will not check for equality

a is equal to b => a=b correct -> a==b

the not equal should be written like this


a != b
NOT like this => a =! b
Assignment Operators

int a, b = 10;
First operand Second operand

a=b
Assignment Operator
Assignment Operators
int a = 5;
• Statements of the form
variable = variable operator expression;
a = a + 5;
can be rewritten as
variable operator= expression;
a += 5;

Try this: int a = 5, b=5, c=15;


a -= 10;
b *= 2;
c /= 5;
Assignment Operators
• Another Examples of other assignment operators
include:
int d=1,e=2,f=3,g=4;

d -= 4 => (d = d - 4)
e *= 5 => (e = e * 5)
f /= 3 => (f = f / 3)
g %= 2 => (g = g % 2)
Assignment Operators
int c=2; d=5, e=10,f=15,g=20;

c += d (c = c + d) => c = 2 + 5
=> c = 7

d -= 4 (d = d - 4) => d = 5 – 4
=> d = 1

e *= 5 (e = e * 5) => e = 10 * 5
=> e = 50

f /= d (f = f / d) => f = 15 / 5
=> f = 3

g %= 9 (g = g % 9) => g = 20 % 9
=> g = 2
Let’s try to analyze this
Logical Operators and Expressions

 All conditional statements make use of logical


expressions that are true/false statements about
data
 Simple logical expressions are of the form:
(operand operator operand)  expression

 The compiler evaluates logical expressions from


left to right to see if they are true/false
Logical Operators and Expressions
 Simple logical expressions have limited power
 To solve this problem, we can combine simple logical expressions
to get complex logical expressions
 This is useful for more realistic data comparison tasks
 The basic syntax is: (expression operator expression)
 Expression can be a simple or complex logical expression
• 
 The C++ logical operators are:
• && and
• || or
• ! not
Logical Operators and Expressions
The NOT Operator
 The not operator in C++ reverses the value of any logical
expression
• Logically “not true” is same as “false”
• Logically “not false” is same as “true”
 The C++ syntax for the not operator is: !expression
bool a=true;
bool b= !a;
=> then
b = not true;therefore b=false
 This is a “unary” operator since there is just one expression to
the right of the not operator
Logical Operators and Expressions

The NOT Operator


 Examples with integer variables a = 7 and b = 3

Condition Answer
• (a > b) is true !(a > b) is false
• (a <= b) is false !(a <= b) is true
• (a == b) is false !(a == b) is true
• (a != b) is true !(a != b) is false
Logical Operators and Expressions
The NOT Operator
Let’s try to analyze this
The NOT Operator
Logical Operators and Expressions
The OR operator

Truth tables are often be used to enumerate all


possible values of a complex logical expression
• We make columns for all logical expressions
• Each row illustrates one set of input values
Logical Operators and Expressions
The AND operator

Truth tables are often be used to enumerate all


possible values of a complex logical expression
• We make columns for all logical expressions
• Each row illustrates one set of input values
Logical Operators and Expressions
x y x ||y
int a = 5, b = 3, c =9;
d e true
boolean d,e,f,g;
e f true

d = a > b; => d = 5 > 3 f d true


g f false
=> d = true
TRY THIS
x y x &&y
e = c != (a+b); => e = 9!=8
d e true
=> e = true
e f false
f d false
f = !d; => f = !true
g f false
=> f = false
g = false; => g = false
Let’s try to analyze this
Logical Operators and Expressions

 Java and other curly braces language like c++ has a


feature called “conditional evaluation” that will
stop the evaluation early in some cases
• (exp1 && exp2) will be false if exp1 is false
• (exp1 || exp2) will be true if exp1 is true
• In both cases, we do not need to evaluate exp2
Complex Logical Expressions
 Java evaluates complex logical expressions from
left to right
• (exp1 && exp2) will be true if both exp are true
• (exp1 && exp2 && exp3) will be true if all exp are true
• (exp1 || exp2 || exp3) will be true if any exp is true

int a = 5, b = 3, c =9; boolean d=false;


1. (a > b) && (c != (a+b))
true && true => true

2. (a > b) && (c != (a+b)) && d


true && true && false => false

3. !d || c<(a+b) || false
true || false || false => true
Increment and Decrement Operators
increment operator (++) adds 1 to its operand.
example:
int c=2;
c++ => c=c+1 => c=2+1 => c=3
Preincrement
When the operator is used before the variable (++c )
Variable is changed, then the expression it is in is evaluated.
Posincrement
When the operator is used after the variable (c++)
Expression the variable is in executes, then the variable is changed.
Increment and Decrement Operators
Decrement operator (--) less 1 to its operand.
example:
int c=2;
c-- => c=c-1 => c=2-1 => c=1
Predecrement
When the operator is used before the variable (--c )
Variable is changed, then the expression it is in is evaluated.
Posdecrement
When the operator is used after the variable (c--)
Expression the variable is in executes, then the variable is changed.
Increment and Decrement Operators
Increment and decrement is not only for adding or subtracting 1 from the
current value of the variable. They can be any value you wish to add or
subtract from the value of the control variable. The following are examples
of the increment and decrement by other numerals aside from 1.
UPDATE CURRENT VALUE OF x
int x = 5; x = x + 2; 7
x -= 5; 2
x -= 9; -7
x += 2; -5
x += 10; 5
x -= 6; -1
x = x – 12; -13
x += 7; -6
x -= 14; -20

You might also like