Slide 03 - Operators and Expression
Slide 03 - Operators and Expression
a-b 7
a*b 30
a-b 10.5
a*b 25.0
a/b 6.25
a+b 131
a+1 66
a + ‘A’ 130
a + ‘1’ 114
int a, b;
a = 11;
b = -3;
Follow
basic
rules of
a+b 8 algebra
a-b 14
a*b -33
a/b -3
float
Left
int 11.995
Right
float
int
3 float
Left
3.0
int
11
;
Type Cast
• To transform the type of a variable temporarily.
• To do so, the expression must be preceded by the
name of the desired data type, enclosed in
parentheses
int number;
(float) number;
Valid or Invalid?
i = 7;
f = 8.5;
result = (i + f) % 4; Invalid
• Type conversion 2
Valid or Invalid?
num % 2;
((int)num) % 2;
Relational Operators
• Use to compare two values.
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Relational Operators…
• These six operators are used to form logical
expressions, which represent conditions that are
either true or false.
• The resulting expressions will be of type integer
Example Description
A<B True if A is less than B else False
A <= B True if A is less than or equal B else False
A>B True if A is greater than B else False
A >= B True if A is greater than or equal B else False
A == B True if A is equals to B else False
A != B True if A is equals to B else False
i=1
True or False j=2
k=3
i<j true 1
(1 + j) >= k true 1
(j + k) > (i + 5) false 0
i=1
True or False j=2
k=3
k != 3 false 0
j == 2 true 1
(j + k) >= (i + 5) false 0
Precedence
Each One is Complement of Another
Simplified Expression
Logical Operation
• There are three kinds of logical operators.
Operator Precedence
! Highest
> >= < <=
== !=
&&
|| Lowest
Assignment Operators
• Use to assign the result of an expression to a variable.
• Most common “=“
• Remember: = and == are not same!
• Other five are:
• +=
• -=
• *=
• /=
• %=
Shorthand Assignment Operator
; ;
; ;
Shorthand Assignment Operator
• ++ and --
• The ++ add 1 to the operand and -- subtracts 1.
• Both are unary operators and takes the form:
++m; or m++;
--m; or m--;
m = 5;
m = 6
++m;
m = 5;
m = 6
m++;
m = 5; m = 6
y = ++m; y = 6
m = 5; m = 6
y = m++; y = 5
• Program
Conditional Operators
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
We will learn
later!
Special Operators
• C supports some special operators of interest
– Comma operator ( , )
– Sizeof operator (sizeof(v))
– Pointer operators ( & and * )
– Member selection operator ( . and ->)
The Comma Operator
• The comma operator can be used to link the related
expressions together.
x = 10;
y = 5; value = (x=10, y=5, x+y;)
value = x+y;
t = x, t = y, t = z; z
We will learn
later!
Operator Precedence and Associativity
• Precedence is used to determine how an expression
involving more than one operator is evaluated.
• There are distinct levels of precedence
• An operator may belong to one of these levels
• The operators at higher level of precedence are
evaluated first.
• The operators of the same precedence are evaluated
either from ‘left to right’ or from ‘right to left’.
• This is known as associativity property of an operator.
Operators Precedence
Some Computational Problem
• Computer gives approximate values for real
numbers that can cause serious problem.