Download - Chapter 3 dti2143
![Page 1: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/1.jpg)
EXPRESSIONS AND OPERATORS
CHAPTER 3
DTI 21431
![Page 2: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/2.jpg)
2
CONTENTS
1. Arithmetic Operators
2. Assigning Mathematical Operator Combinations
3. Unary Sign Operators
4. Increments And Decrement Operator
5. Conditional Operator
6. Logical Expressions
7. Logical Operator
CHAPTER 4
2
![Page 3: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/3.jpg)
INTRODUCTION
1. In any programming languages, we need operators to construct statements to do an operation and calculations.
2. In this chapter, you will be introduced to several operators such as:
a. Unary Operatorsb. Binary Operatorsc. Ternary Operators
3. Operator can be applied to variables or any elements in an expression to produce some kind of computation or action.
3
![Page 4: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/4.jpg)
ARITHMETIC OPERATORS
1. We can form more complicated arithmetic expressions by using arithmetic operators.
2. There are two types of arithmetic operators in c such as:
a) Unary arithmetic operators• Required one operand.
b) Binary arithmetic operators• Required two operand.
4
![Page 5: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/5.jpg)
1. Examples of Unary arithmetic operators
2. Binary arithmetic operators
Operators Symbol
1. Unary Plus +
2. Unary Minus -
3. Increment Operators ++
4. Decrement Operators --
Operators Symbol
1. Additional Operator +
2. Subtraction Operator -
3. Multiplication Operator *
4. Division Operator /
5. Modulus Operator (Remainder of division) % 5
![Page 6: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/6.jpg)
Example 1#include <stdio.h>#include <conio.h>int main(void){int num= 20;int val=30;num=val+5; /* num=30+5*/val=num-val; /*val=35-30*/num=num+2; /*num=35-5*/val=30/7; /*val=30/7*/getch();return 0;} 6
![Page 7: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/7.jpg)
Example 2#include <stdio.h>#include <conio.h>int main(void){int num= 20;printf (“%d\n\n”, num);int val=30;printf (“%d\n\n”, val);num=val+5; printf (“%d\n\n”, num);val=num-val;printf (“%d\n\n”, val);num=num+2;printf (“%d\n\n”, num);val=30/7;printf (“%d\n\n”, val);getch();return 0;}
Output
20
30
35
5
37
4
7
![Page 8: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/8.jpg)
Example 3
#include <stdio.h>#include <conio.h>int main(void){int a,b,c,d,result;a=10;b=20;c=15;d=8;result=a*b/(-c*31%13)*d;printf ("%d",result);getch();return 0;}
Output
-160
8
![Page 9: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/9.jpg)
How to Solves Example 3?
• The expression will evaluated 6 steps
a=10,b=20,c=15,d=8,e=40,a * b / (-c * 31 % 13 )* d
Step Operator Reduced Expression
1. Unary - a * b / (-15 * 31 % 13 ) * d
2. * a * b / (-465 % 13 ) * d
3. % a * b / (-10) * d
4. * 200 / (-10) * d
5. / -20 * d
6. * -1609
![Page 10: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/10.jpg)
Example 3
#include <stdio.h>#include <conio.h>int main(void){int num =5;num -=3;num +=4;printf ("%d", num);getch();return 0;}
Output
6
10
![Page 11: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/11.jpg)
Example 4
Operator Syntax Meaning
*= price *=2.5; price= price * 2.5;
/= total /=2;
%= month %=2; month=month%=2;
+= bonus +=800;
-= balance -=sum +tax;
total = total / 2;
bonus=bonus+800;
balance =balance- sum + tax
11
![Page 12: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/12.jpg)
INCREMENTS AND DECREMENT OPERATOR
• In C, we have two special operators for incrementing or decrementing a variable by one.
• The operators are:i. Unary Increment operators : ++
a) ++i (prefix)
b) i++ (postfix)
ii. Unary Decrement Operators : --a) --i (prefix)
b) i-- (postfix)
12
![Page 13: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/13.jpg)
a) Unary Increment operators : ++
» To increment a variable by 1» Also used as counter in loop structure» Eg: i=i+1; or» i +=1;
b) Unary Decrement Operators : --
» To decrement a variable by 1» Eg: i=i-1 or» i -=1;
13
![Page 14: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/14.jpg)
INCREMENTS OPERATOR
++i (prefix) i++(postfix)
In the latter case, the operator comes just in front of the variable.
Eg:++grade;++mark;++a;
In the former case, we place the operator after the variable.
Eg:grade++;mark++;a++;
Incrementing “i” value. Use “i” value in expression.
Use new i value in expression
Eg:int i=0;++i; /* 1 */printf(“%d”, i);
Incrementing i value (before expression executes)
Eg:int i=0;i++; /* 0 + 1(in memory) */printf(“%d”, i);
/*Output : 1*/ /*Output : 1*/
14
![Page 15: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/15.jpg)
LOGICAL EXPRESSIONS
• C provides logical operators that are used to combine expressions into logical expressions.
15
![Page 16: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/16.jpg)
RELATIONAL AND EQUALITY OPERATOR
OPERATOR MEANING CONDITION VALUE
< Less Than Power<MAX_POW 0(false)
<= Less than or equal to
x<=0 1(true)
> Greater Than x>=y 0(false)
>= Greater than or equal to
item>MIN_ITEM 1(true)
== Equal to mom_or_dad==‘M’ 1(true)
!= Not equal to num !=SENTINEL 0(false)
16
![Page 17: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/17.jpg)
Example 1
OPERATOR CONDITION VALUE
1. == (a+b)==c 0
2. != a!=b 1
3. < b<a
4. > a>c
5. <= (b+c)<=a
6. >= c>=b
a = 6;b = 1;c = - 2;
1
1
0
0
17
![Page 18: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/18.jpg)
LOGICAL OPERATOR
• The purpose of logical operator is to combine and check the results of logical expressions.
OPERATOR MEANING
! NOT
&& AND
|| OR
18
![Page 19: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/19.jpg)
EXAMPLE 1
EXPRESSION VALUE (TRUE /FALSE)
1. ( 5 = = 5 ) && ( 7 ! = 2 ) 1(TRUE)
2. ( 5 > 4 ) | | ( 9 < 3 ) 1(TRUE)
3. ( 2 > 2 ) && ( 7 = = 7 )
4. ! ( 7= = 6 )
5. ( 1 = = 2 ) | | ( 6 > = 9 )
0(FALSE)
1(TRUE)
0(FALSE)
19
![Page 20: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/20.jpg)
EXAMPLE OF QUESTIONS
1) Given:
int val, x = 4, y = 3, z = 10;val =(x+y>=3*z) == (x!=3*z+y);
The value of val is:
a) 1
b) 0
c) 10
d) 1720
ANSWER
B
![Page 21: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/21.jpg)
2) Given:
int val, a = 1, b = 2, c = 3, d = 4;val = d+a%c*b;
The value of val is:
a. 6
b. 5
c. 0
d. 121
ANSWER
A
![Page 22: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/22.jpg)
3)
int cost = 2;
int price = 5;
cost += ++price * 2;
What is answer for cost?
22
ANSWER
14
![Page 23: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/23.jpg)
4)
Assume count = 21.
What is the current value of
count at each of these statements?
a) count-- =……….
b) ++count =……….
23
ANSWER
•21•21
![Page 24: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/24.jpg)
5)
#include<stdio.h>#include<conio.h>int main(){ int n01=39,n02=100; printf("%d",++n01); printf("\n%d",n01--+2); printf("\n%d",(--n01)+(++n02)); printf("\n%d",n01); getch (); return 0; }
![Page 25: Chapter 3 dti2143](https://reader036.vdocument.in/reader036/viewer/2022062418/5558693cd8b42a8d018b49be/html5/thumbnails/25.jpg)
6)
#include<stdio.h>#include<conio.h>int main(){ int x=6,y=6; printf("%d",x++); printf("%d",x); y--; printf("%d %d",x,y); getch (); return 0; }