DTI2143 Computer Programming<br />1<br />Chapter 3<br />Expression<br />&<br />Operators <br />
Aim<br />2<br />To give understanding on:<br /><ul><li>Expression and operator concept
math.h and stdlib.h built-in function</li></ul>Objective<br />Students should be able to:<br /><ul><li>understand concepts...
4<br />Introduction to Expression<br />Process involves in money withdrawal scenario<br />balance – withdrawed money = cur...
What is Expression?<br />5<br />Introduction to Expression<br />Combination or more than one variable or <br />Constant (o...
6<br />Arithmetic Expression<br />Known as<br />Arithmetic Expression<br />Mathematic Expression<br />using<br />Arithmeti...
Unary Operator<br />7<br />Arithmetic Expression<br />Unary Operator<br />Operates for one operand<br />Computer memory ce...
Unary Operator<br />8<br />Arithmetic Expression<br />Example 1:<br />int  A = 5;<br />++A; <br />printf(“%d”, A);        ...
Unary Operator<br />9<br />Arithmetic Expression<br />Example 2:<br />int  A ;<br />A=6;<br />printf(“%d”,3 + --A);       ...
Unary Operator<br />10<br />Arithmetic Expression<br />Example 3:<br />Given the value of  num1 = 8 .Determine the value o...
11<br />
Binary Operator<br />12<br />Arithmetic Expression<br />Located between constants or <br />variables or both combination<b...
Binary Operator<br />13<br />Arithmetic Expression<br />Multiplication<br />Use symbol  “ * ”<br />example<br />A  *    z<...
Binary Operator<br />14<br />Arithmetic Expression<br />Divide<br />Use symbol  “/”<br />example<br />A   /    z<br />oper...
Binary Operator<br />15<br />Arithmetic Expression<br />Modulus<br />Use symbol  “%”<br />example<br />A   %    z<br />ope...
Binary Operator<br />16<br />Arithmetic Expression<br />Example:<br />int  A, B;<br /> float C;<br /> A= 2;<br /> B= 5;<br...
Assignment Statement<br />17<br />Arithmetic Expression<br /><ul><li>Used to store value/result of process to a variable
Use operator symbol  =</li></ul>Assignment statement<br />Double assignment<br />statement<br />Compound assignment<br />s...
Assignment Statement<br />18<br />Arithmetic Expression<br /><ul><li>Format /sintax :</li></ul>variable = value;<br />	var...
19<br />Arithmetic Expression<br />Compound Assignment Statement<br /><ul><li>Use more than one operator (=)
Example :</li></ul>int a = b= c = d = e = 250;<br />int b =2, number =0, total = 0,average =3;<br /> number = b++ = 10;<br...
20<br />Arithmetic Expression<br />Compound Assignment Statement<br />Function<br /><ul><li>To combine two different opera...
To simplify arithmetic operator
Original function of operator does not affected
Allowed combination are as follow:</li></ul>operator:<br />+= , -= , *= , /= , %=<br />
Compound Assignment Statement<br />21<br />Arithmetic Expression<br />Example :<br />
Arithmetic Operator Precedence Rules<br />22<br />Arithmetic Expression<br />Compiler will follows the following precedenc...
Arithmetic Operator Precedence Rules<br />23<br />Arithmetic Operator<br />Example:<br />5 + 2 * 6 – 4 / 2<br />5  +  12  ...
Arithmetic Operator Precedence Rules<br />24<br />Arithmetic Expression<br />Example:<br />3. Prefix unary arithmetic expr...
Arithmetic Operator Precedence Rules<br />25<br />Arithmetic Expression<br />Example:<br />3. Prefix unary arithmetic expr...
26<br />Arithmetic Expression<br />Mathematic Library Function<br /><ul><li>Diwakiliolehperpustakaanpiawaimatematkiaitumath.h
Dipanggilbersama#include
Antarafungsiperpustakaanmatematik yang penting:</li></li></ul><li>27<br />Arithmetic Expression<br />Mathematic Library Fu...
Exercise:<br />28<br />Arithmetic Expression<br />Convert the following mathematic expression to a valid arithmetic expres...
Exercise:<br />29<br />Arithmetic Expression<br />      Assume i,j and k are integer variables with i = 5 and j=3. Determi...
30<br />Relation Expression<br />Relational<br />expression<br />use<br />Relational operator<br />Combination of more tha...
Relational Operator<br />31<br />Relation Expression<br />
32<br />Relation Expression<br />P/s:<br />a, b and  c are variables,<br />Replace with the given values<br />Example 1:<b...
33<br />Relation Expression<br />Example 2 :<br />int a=6, b =1, c = -2;<br />  3)	b < a			4) b + c <= a<br />	1 < -2			  ...
34<br />Relation Expression<br />P/s:<br />Relational operator has less priority than other operators.<br />Start evaluati...
35<br />Relation Expression<br />An example program which uses relational expression<br />#include <stdio.h><br />void mai...
36<br />Logical Expression<br />Logical Operator<br />Logical expression<br />use<br />Combination of one or more expressi...
37<br />Logical Expression<br />Logical Operator<br />Logical operator && dan || is used between 2 or more<br />relational...
38<br />Logical Expression<br />Logical operator truth table for AND<br />AND (&&)<br />
Upcoming SlideShare
Loading in...5
×

Dti2143 chapter 3 arithmatic relation-logicalexpression

1,125

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,125
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
84
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dti2143 chapter 3 arithmatic relation-logicalexpression

  1. 1. DTI2143 Computer Programming<br />1<br />Chapter 3<br />Expression<br />&<br />Operators <br />
  2. 2. Aim<br />2<br />To give understanding on:<br /><ul><li>Expression and operator concept
  3. 3. math.h and stdlib.h built-in function</li></ul>Objective<br />Students should be able to:<br /><ul><li>understand concepts and fundamentals in expression/</li></ul> operator.<br /><ul><li>write expression in C programming language</li></li></ul><li>3<br />Introduction to Expression<br />Given the following statement :<br />2x + 3 – z = y<br />expression<br />
  4. 4. 4<br />Introduction to Expression<br />Process involves in money withdrawal scenario<br />balance – withdrawed money = current balance<br />Expression in C :<br />bakiTerkini =wangKeluar – bakiSemasa;<br />
  5. 5. What is Expression?<br />5<br />Introduction to Expression<br />Combination or more than one variable or <br />Constant (operand) which separated by <br />operator<br />Expression<br />Operator<br />example<br />x + 3 - z<br />Operand<br />Consists of<br />arithmetic<br />relational<br />logical<br />
  6. 6. 6<br />Arithmetic Expression<br />Known as<br />Arithmetic Expression<br />Mathematic Expression<br />using<br />Arithmetic Operator<br />Represents by<br />Represents by<br />Unary operator<br />Binary operator<br />
  7. 7. Unary Operator<br />7<br />Arithmetic Expression<br />Unary Operator<br />Operates for one operand<br />Computer memory cell<br />example<br />-20<br />a = -20;<br />a<br />b = +15;<br />b<br />+15<br />
  8. 8. Unary Operator<br />8<br />Arithmetic Expression<br />Example 1:<br />int A = 5;<br />++A; <br />printf(“%d”, A); output ?<br />A--;<br />printf(“%d”,A); output ?<br />A++;<br />printf(“%d”,A); output ?<br />
  9. 9. Unary Operator<br />9<br />Arithmetic Expression<br />Example 2:<br />int A ;<br />A=6;<br />printf(“%d”,3 + --A); output ?<br />printf(“%d”, A); output ?<br />A=6;<br />printf(“%d”, 3 + A--); output ?<br />printf(“%d”, A); output ?<br />
  10. 10. Unary Operator<br />10<br />Arithmetic Expression<br />Example 3:<br />Given the value of num1 = 8 .Determine the value of num2 after the execution for each of the following statements: <br />num2 = num1++ - 2;<br />num2 = num1;<br />num2 = ++num1 – 3;<br />num2 = num1-- +1;<br />
  11. 11. 11<br />
  12. 12. Binary Operator<br />12<br />Arithmetic Expression<br />Located between constants or <br />variables or both combination<br />Binary Operator<br />example<br />A+ z<br />operator<br />operand<br />
  13. 13. Binary Operator<br />13<br />Arithmetic Expression<br />Multiplication<br />Use symbol “ * ”<br />example<br />A * z<br />operator<br />operand<br />Mathematic<br />Arithmetic Expression<br />2x + y<br />2 * x + y<br />
  14. 14. Binary Operator<br />14<br />Arithmetic Expression<br />Divide<br />Use symbol “/”<br />example<br />A / z<br />operator<br />operand<br />Mathematic<br />Arithmetic Expression<br />2 : y<br />2 / y<br />
  15. 15. Binary Operator<br />15<br />Arithmetic Expression<br />Modulus<br />Use symbol “%”<br />example<br />A % z<br />operator<br />operand<br />Return a balance when 2 numbers is divided<br />Can only be used with an integer variable<br />
  16. 16. Binary Operator<br />16<br />Arithmetic Expression<br />Example:<br />int A, B;<br /> float C;<br /> A= 2;<br /> B= 5;<br /> C= 2.4;<br /> B% A;<br /> C % A;<br />Valid! Answer is 1<br />Invalid! C is float<br />
  17. 17. Assignment Statement<br />17<br />Arithmetic Expression<br /><ul><li>Used to store value/result of process to a variable
  18. 18. Use operator symbol =</li></ul>Assignment statement<br />Double assignment<br />statement<br />Compound assignment<br />statement<br />
  19. 19. Assignment Statement<br />18<br />Arithmetic Expression<br /><ul><li>Format /sintax :</li></ul>variable = value;<br /> variable = constant; or variable = variable;<br /> variable = expression;<br /><ul><li>Example :</li></ul>1.average= ( 6 + 5) * 4;<br />2.grossSalary = 1500;<br />nettSalary = grossSalary + 200;<br />3.price= 50.00;<br /> pay = price;<br />`<br />44<br />average<br />1500<br />grossSalary<br />1700<br />nettSalary<br />50.00<br />price<br />pay<br />50.00<br />
  20. 20. 19<br />Arithmetic Expression<br />Compound Assignment Statement<br /><ul><li>Use more than one operator (=)
  21. 21. Example :</li></ul>int a = b= c = d = e = 250;<br />int b =2, number =0, total = 0,average =3;<br /> number = b++ = 10;<br />int age = workHour = 0;<br />
  22. 22. 20<br />Arithmetic Expression<br />Compound Assignment Statement<br />Function<br /><ul><li>To combine two different operator together.
  23. 23. To simplify arithmetic operator
  24. 24. Original function of operator does not affected
  25. 25. Allowed combination are as follow:</li></ul>operator:<br />+= , -= , *= , /= , %=<br />
  26. 26. Compound Assignment Statement<br />21<br />Arithmetic Expression<br />Example :<br />
  27. 27. Arithmetic Operator Precedence Rules<br />22<br />Arithmetic Expression<br />Compiler will follows the following precedence to execute the arithmetic expression based on priority.<br />
  28. 28. Arithmetic Operator Precedence Rules<br />23<br />Arithmetic Operator<br />Example:<br />5 + 2 * 6 – 4 / 2<br />5 + 12 - 4 / 2<br /> 5 + 12 - 2<br /> 17 - 2<br /> 15<br />2. 3 * 4 / 2 + ( 3 –1)<br /> 3 * 4 / 2 + 2<br /> 12 / 2 + 2<br /> 6 + 2<br /> 8<br />
  29. 29. Arithmetic Operator Precedence Rules<br />24<br />Arithmetic Expression<br />Example:<br />3. Prefix unary arithmetic expression<br />intkira = 5, nilaipertama = 10;<br />nilai_kedua = 5 * --kira + nilai_pertama;<br />printf(“%d %d”, kira, nilai_kedua);<br />Output:<br />4 30<br />
  30. 30. Arithmetic Operator Precedence Rules<br />25<br />Arithmetic Expression<br />Example:<br />3. Prefix unary arithmetic expression<br /> int kira = 5, nilai pertama = 10;<br /> nilai_kedua = 5 * kira-- + nilai_pertama;<br /> printf(“%d %d”, kira, nilai_kedua);<br />Output:<br />4 35<br />
  31. 31. 26<br />Arithmetic Expression<br />Mathematic Library Function<br /><ul><li>Diwakiliolehperpustakaanpiawaimatematkiaitumath.h
  32. 32. Dipanggilbersama#include
  33. 33. Antarafungsiperpustakaanmatematik yang penting:</li></li></ul><li>27<br />Arithmetic Expression<br />Mathematic Library Function<br />Example:<br />#include<stdio.h><br />#include <math.h><br />void main()<br />{<br /> int x = 16, y ;<br /> y = sqrt(x);<br /> printf(“%d”,y);<br />}<br />Output :<br />4<br />
  34. 34. Exercise:<br />28<br />Arithmetic Expression<br />Convert the following mathematic expression to a valid arithmetic expression :<br /> a) b = 3 + bb) x = (a – b)(a – c2)<br /> a + 4<br /> c) d = (3e – d) - ( 4 – 3c3 ) d) r = 2s + 3(s – 9)<br /> x – 9 4y s<br />Given a= 3, b = 5, c=1. What is the output of the following expression?<br /> a. ( 6 * c – 6 / a) - b b. (5 * c) +( a* b / b)<br /> c. ++a d. c + a * c / (3 * c)<br />
  35. 35. Exercise:<br />29<br />Arithmetic Expression<br /> Assume i,j and k are integer variables with i = 5 and j=3. Determine what is the value for each of the following statement:<br /> a) k = j++; d) k = ++j; <br /> b) k = i * j--; e) k = i * --j;<br /> c) k = j + i * j++; f) k = 27 / j++ - 16 % i;<br />
  36. 36. 30<br />Relation Expression<br />Relational<br />expression<br />use<br />Relational operator<br />Combination of more than one statement<br />variable vs variable<br />Can consists of<br />variable vs constant<br />produce<br />constant vs constant<br />0 (if false)<br />1(if true)<br />
  37. 37. Relational Operator<br />31<br />Relation Expression<br />
  38. 38. 32<br />Relation Expression<br />P/s:<br />a, b and c are variables,<br />Replace with the given values<br />Example 1:<br />int a=6, b =1, c = -2;<br />a+ b == c 2) a != b<br /> 6 + 1== -2 6 != 1<br /> 7 == -2<br />Answer: 0(False) Answer : 1 (True)<br />
  39. 39. 33<br />Relation Expression<br />Example 2 :<br />int a=6, b =1, c = -2;<br /> 3) b < a 4) b + c <= a<br /> 1 < -2 1 + -2 < 6<br /> -1 < 6<br />Answer: 0 (False) Answer : 1 (True)<br />
  40. 40. 34<br />Relation Expression<br />P/s:<br />Relational operator has less priority than other operators.<br />Start evaluating from left to right.<br />Example 3:<br />int a=10, b = 3, c = 7;<br />(a+b >= 3*c)==( a != 2*c+b)<br />(10+3 >= 3*7)==(a != 2*c+b)<br />(13 >= 21)==(10 != 14+3)<br />(13 >= 21)==(10 != 17)<br />0 == 1<br />0 (false)<br />
  41. 41. 35<br />Relation Expression<br />An example program which uses relational expression<br />#include <stdio.h><br />void main()<br />{ int age;<br />printf(“nPlease enter your age >>”);<br />scanf(“%d”,&age);<br /> if (age > 21)<br />printf(“nYou are qualified to vote”);<br />}<br />Relational expression<br />
  42. 42. 36<br />Logical Expression<br />Logical Operator<br />Logical expression<br />use<br />Combination of one or more expressions<br />Can consists of<br />Relational expr. vs logical expr.<br />Relational expr. vs variable<br />produces<br />Relational expr. vs constant<br />0 (if false)<br />1(if true)<br />
  43. 43. 37<br />Logical Expression<br />Logical Operator<br />Logical operator && dan || is used between 2 or more<br />relational expression<br />
  44. 44. 38<br />Logical Expression<br />Logical operator truth table for AND<br />AND (&&)<br />
  45. 45. 39<br />Logical Expression<br />Logical operator truth table for OR<br />OR (||)<br />
  46. 46. 40<br />Logical Expression<br />Logical operator truth table for NOT<br />NOT(!)<br />
  47. 47. 41<br />Logical Expression<br />Example 1:<br />Evaluate the following logical expression:<br />(2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) || ( 2 * 3 == 6)<br /> 1 && 1 (1 < 2) || (6 == 6)<br /> 1 1 || 1<br /> 1<br />
  48. 48. 42<br />Logical Expression<br />Example 2:<br />Evaluate the following logical expression:<br />Given a = 3, b = 4;<br />c) !((5 * b <= 23 - a )) d) ! ((b +3 != 8) &&( 3 * a < 2))<br /> !((5 * 4 <= 23 – 3)) !(( 7 != 8 ) && ( 9 < 2 ))<br /> !(20 <= 20) ! ( 1 && 0 )<br /> !(1) ! ( 0)<br /> 0 1<br />
  49. 49. 43<br />Logical Expression<br />Logical Expression<br />An example program which using Logical Expression:<br />#include <stdio.h><br />void main()<br />{ int mark;<br />printf(“nEnter your mark >>”);<br />scanf(“%d”,&mark);<br /> if (mark >= 85 && mark <= 100)<br />printf(“nGred A”);<br /> else if( mark >=80 && mark <=84)<br />printf(“nGred A-”);<br />}<br />
  50. 50. Exercise:<br />44<br />Logical Expression<br />Given with i=2, j = 5 and k = 15. Evaluate each of the following expression:<br />a) i > j – k g) k == j + I * j<br /> b) i != k h) k <=k /j<br /> c) (i >= 1) && (j == 5) i) (j < i) || (k > j)<br /> d) !( i > j) j) (i >0) && (j <k) || (k <i)<br /> e) i < j < k k) i * k < k / j<br /> f) (i < j) && (j < k) i) i – j > k<br />Complete the following statements with suitable logical expression.<br />int angka1,angka2;<br /> if (angka1 is less than or equal to angka2)<br />printf(“%d is less than or equal to %d”,angka1,angka2);<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×