ITPF01 Week4 Operators and Expressions
ITPF01 Week4 Operators and Expressions
Operators and
Expressions
Melgine M. Bauat, MSIT
Instructor
Topic Outline
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
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
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;
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
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
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