Professional Documents
Culture Documents
1
CONTENTS
Relational Operators
Logical Operators
Unary Arithmetic Operators
Assignment Operator
Increment & Decrement
Ternary Operator
Conditional Operator
2
RELATIONAL OPERATORS
3
RELATIONAL OPERATORS
Notes
"==" equality operator is different from the "=", assignment
operator.
"==" operator on float variables is tricky because of finiteprecision.
5
LOGICAL OPERATORS
2 OR. || operator
1 2==3 ||’A’==’A’;/ * evaluatesto1 */
2 2.99 >=3 | | 0;/ * evaluatesto0 */
3 NOT. ! operator
1 ! ( 3==3);/ * evaluatesto0 */
2 ! ( 2.99 >=3);/ * evaluatesto1 */
6
LOGICAL OPERATORS
7
TRUTH TABLES
Negation
x Result
!0 1
!1 0
8
CONDITIONAL EXPRESSION
General syntax.
1 i f (cond)
2 x= <expra>;
3 else
4 x= <exprb>;
9
EXERCISES
• Q1: Write a program that asks the user to enter two numbers,
and prints their sum, product, difference, quotient and
remainder.
10
UNARY ARITHMETIC OPERATORS
• i = +1;
• j = -i;
12
OPERATOR PRECEDENCE
• Highest: + - (unary)
• * / %
• Lowest: + - (binary)
• Examples:
• i + j * k is equivalent to i + (j * k)
• -i * -j is equivalent to (-i) * (-j)
• +i + j / k is equivalent to (+i) + (j / k)
13
SIMPLE ASSIGNMENT
14
SIMPLE ASSIGNMENT
• int i;
• float f;
• i = 72.99f; /* i is now 72 */
• f = 136; /* f is now 136.0 */
15
LVALUES
16
LVALUES
17
COMPOUND ASSIGNMENT
18
COMPOUND ASSIGNMENT
• i = i + 2; i += 2;
• -=
• /=
• *=
• %=
19
EXAMPLE
20
INCREMENT AND DECREMENT BY 1
• i = i + 1; i += 1; i++;
• i = i – 1; i -= 1; i--;
21
INCREMENT AND DECREMENT OPERATORS
22
PRE AND POST INCREMENT
24
PRE AND POST DECREMENT
• i = 1;
• printf("i is %d\n", i--); /* prints "i is 1" */
• printf("i is %d\n", i); /* prints "i is 0" */
25
EXAMPLE
• int main() {
• int a = 10, b = 100;
• float c = 10.5, d = 100.5;
• printf("++a = %d \n", ++a);
• printf("--b = %d \n", --b);
• printf("++c = %f \n", ++c);
• printf("--d = %f \n", --d);
• return 0; }
26
TABLE OF ARITHMETIC OPERATORS
DISCUSSED SO FAR:
27
EXERCISE
• Example:
• i = 1;
• j = 2;
• k = ++i + j++;
28
TERNARY OPERATOR (?:)
29
EXAMPLE
• if (x%2==1)
== isodd = x%2==1 ? 1 : 0;
• isodd = 1
• else
• isodd = 0
30
CONDITIONAL OPERATOR
• isodd = x%2==1 ? 1 : 0;
31
THE SIZEOF OPERATOR
• The sizeof is an unary operator which returns the size of data (constant,
variables, array, structure etc).
• Example
• main(){
• int a; float b; double c; char d;
32
RELATIONAL OPERATOR EXAMPLE
•i < j < k
33
EXERCISE
• Write a program to prompt the user to input the integral value of a and print
out the result as shown below:
•
Result:
The value of a is: 10
……………………..
The value of ++a is: 11
Now the value of a is: 11
34
QUESTIONS
35