Upcoming SlideShare
×

# Chapter 3 dti2143

766 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
766
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
74
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Chapter 3 dti2143

1. 1. EXPRESSIONS AND OPERATORS<br />CHAPTER 3<br />DTI 2143<br />1<br />
2. 2. CONTENTS<br />Arithmetic Operators<br />Assigning Mathematical Operator Combinations<br />Unary Sign Operators<br />Increments And Decrement Operator<br />Conditional Operator<br />Logical Expressions<br />Logical Operator<br />2<br />2<br />CHAPTER 4<br />
3. 3. INTRODUCTION<br />In any programming languages, we need operators to construct statements to do an operation and calculations.<br />In this chapter, you will be introduced to several operators such as:<br />Unary Operators<br />Binary Operators<br />Ternary Operators<br />Operator can be applied to variables or any elements in an expression to produce some kind of computation or action.<br />3<br />
4. 4. ARITHMETIC OPERATORS<br />We can form more complicated arithmetic expressions by using arithmetic operators.<br />There are two types of arithmetic operators in c such as:<br />Unary arithmetic operators<br /><ul><li>Required one operand.</li></ul>Binary arithmetic operators<br /><ul><li>Required two operand.</li></ul>4<br />
5. 5. Examples of Unary arithmetic operators<br />Binary arithmetic operators<br />5<br />
6. 6. Example 1<br />#include <stdio.h><br />#include <conio.h><br />int main(void)<br />{<br />int num= 20;<br />int val=30;<br />num=val+5; /* num=30+5*/<br />val=num-val; /*val=35-30*/<br />num=num+2; /*num=35-5*/<br />val=30/7; /*val=30/7*/<br />getch();<br />return 0;<br />}<br />6<br />
7. 7. Example 2<br />#include <stdio.h><br />#include <conio.h><br />int main(void)<br />{<br />int num= 20;<br />printf (“%dnn”, num);<br />int val=30;<br />printf (“%dnn”, val);<br />num=val+5; <br />printf (“%dnn”, num);<br />val=num-val;<br />printf (“%dnn”, val);<br />num=num+2; <br />printf (“%dnn”, num);<br />val=30/7;<br />printf (“%dnn”, val);<br />getch();<br />return 0;<br />}<br />7<br />
8. 8. Example 3<br />#include <stdio.h><br />#include <conio.h><br />int main(void)<br />{<br />int a,b,c,d,result;<br />a=10;<br />b=20;<br />c=15;<br />d=8;<br />result=a*b/(-c*31%13)*d;<br />printf ("%d",result);<br />getch();<br />return 0;<br />}<br />8<br />
9. 9. How to Solves Example 3?<br /><ul><li>The expression will evaluated 6 steps</li></ul> a=10,b=20,c=15,d=8,e=40,<br /> a * b / (-c * 31 % 13 )* d <br />9<br />
10. 10. Example 3<br />#include <stdio.h><br />#include <conio.h><br />int main(void)<br />{<br />int num =5;<br />num -=3;<br />num +=4;<br />printf ("%d", num);<br />getch();<br />return 0;<br />}<br />10<br />
11. 11. Example 4<br />total = total / 2;<br />bonus=bonus+800;<br />balance =balance- sum + tax<br />11<br />
12. 12. INCREMENTS AND DECREMENT OPERATOR<br /><ul><li>In C, we have two special operators for incrementing or decrementing a variable by one.
13. 13. The operators are:</li></ul>Unary Increment operators : ++<br />++i (prefix)<br />i++ (postfix)<br />Unary Decrement Operators : --<br />--i (prefix)<br />i-- (postfix)<br />12<br />
14. 14. Unary Increment operators : ++<br /><ul><li>To increment a variable by 1
15. 15. Also used as counter in loop structure
16. 16. Eg:i=i+1; or
17. 17. i +=1;</li></ul>Unary Decrement Operators : --<br /><ul><li>To decrement a variable by 1
18. 18. Eg:i=i-1 or
19. 19. i -=1; </li></ul>13<br />
20. 20. INCREMENTS OPERATOR<br />/*Output : 1*/<br />/*Output : 1*/<br />14<br />
21. 21. LOGICAL EXPRESSIONS<br /><ul><li>C provides logical operators that are used to combine expressions into logical expressions.</li></ul>15<br />
22. 22. RELATIONAL AND EQUALITY OPERATOR<br />16<br />
23. 23. Example 1<br />a = 6;<br />b = 1;<br />c = - 2;<br />1<br />1<br />0<br />0<br />17<br />
24. 24. LOGICAL OPERATOR<br /><ul><li>The purpose of logical operator is to combine and check the results of logical expressions.</li></ul>18<br />
25. 25. EXAMPLE 1<br />0(FALSE)<br />1(TRUE)<br />0(FALSE)<br />19<br />
26. 26. EXAMPLE OF QUESTIONS<br />1) Given:<br />intval, x = 4, y = 3, z = 10;<br />val =(x+y>=3*z) == (x!=3*z+y);<br />The value of val is: <br />1<br />0 <br />10<br />17<br />20<br />
27. 27. 2) Given:<br />int val, a = 1, b = 2, c = 3, d = 4;<br />val = d+a%c*b;<br /> The value of val is: <br />6<br />5<br />0<br />1<br />21<br />
28. 28. 3)<br />int cost = 2;<br />int price = 5;<br />cost += ++price * 2; <br /> <br />What is answer for cost? <br />22<br />
29. 29. 4)<br />Assume count = 21.<br />What is the current value of<br />count at each of these statements?<br /> <br />a) count-- =……….<br />b) ++count =……….<br />23<br />
30. 30. 5)<br />#include<stdio.h><br />#include<conio.h><br />int main()<br />{<br /> int n01=39,n02=100;<br /> printf("%d",++n01);<br /> printf("n%d",n01--+2);<br /> printf("n%d",(--n01)+(++n02));<br /> printf("n%d",n01);<br /> getch ();<br /> return 0;<br />}<br />
31. 31. 6)<br />#include<stdio.h><br />#include<conio.h><br />int main()<br />{<br />int x=6,y=6;<br />printf("%d",x++);<br />printf("%d",x);<br /> y--;<br />printf("%d %d",x,y);<br />getch ();<br /> return 0;<br /> }<br />