1 / 49

Operators and Expressions

FTSM. Operators and Expressions. Knowledge: Know the types of basic arithmetic operators and their order of precedence Skill: Develop skills in computer arithmetic. Introduction. We use arithmetic expressions to solve most programming problems

arden-glenn
Download Presentation

Operators and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FTSM Operators and Expressions Knowledge: Know the types of basic arithmetic operators and their order of precedence Skill: Develop skills in computer arithmetic Computer Science Department

  2. Introduction • We use arithmetic expressions to solve most programming problems • Arithmetic expressions comprise of operators and operands • There are rules for writing and evaluating arithmetic expressions TK1913-C Programming2

  3. Example: W + Z Operand Operand Operator Operator and Operand What are operator and operand? TK1913-C Programming3

  4. Operator and Operand Which is which? Operator ?? • Example: A / B Operand?? TK1913-C Programming4

  5. Addition (+) Modulus (%) Subtraction (-) Division (/) Multiplication (*) Basic Arithmetic Operators TK1913-C Programming5

  6. Basic Arithmetic Operators • Multiplication, addition and subtraction are the simplest to use • Division is easy, but some precautions need to be taken • Modulus is the one that normally confuses novices So, let’s study in detail the Division and Modulus TK1913-C Programming6

  7. Example: W / Z Integer Division • W and Z are integers Floating Division • W or Z or both are floats Division TK1913-C Programming7

  8. an integer  the result is also an integer an integer Integer Division • Example: 8 / 2 = 4 TK1913-C Programming8

  9. an integer  the result is also an integer an integer Integer Division • Example: 12 / 5 = 2 TK1913-C Programming9

  10. a float the result is a float an integer Floating Division • Example: 12.0 / 5 = 2 TK1913-C Programming10

  11. Something to ponder … What will be the answer if an integer is divided by 0? How about if one of the operands is a negative integer? TK1913-C Programming11

  12. Modulus • It returns the remainder that occurs after performing the division of 2 operands • Rule: Operands must be integers TK1913-C Programming12

  13. the result is the remainder of 12/5 an integer result remainder an integer Modulus • Example: 12 % 5 = 2 2 5 12 10 2 TK1913-C Programming13

  14. the result is the remainder of 7/3 an integer result remainder an integer Modulus • Example: 7 % 3 = 1 2 3 7 6 1 TK1913-C Programming14

  15. a float INVALID! an integer Modulus • Example: 12.0 % 3 = ?? TK1913-C Programming15

  16. Something to ponder … The earlier expressions contain only one operator at a time. What if the expression contains more than one operator? TK1913-C Programming16

  17. Arithmetic Expression • An expression may contain 2 or more arithmetic operators • Main issue: ORDER OF PRECEDENCE TK1913-C Programming17

  18. Arithmetic Expression • Examples: 5 + 6 = 11 5 + 6 * 2 = 17 = 22 or 17? 2.5 + 6 – 2 * 2 = ?? = ?? 12 / 6.0 – 2 * 2 TK1913-C Programming18

  19. Arithmetic Expression Order of Precedence: High:* / % Low:+ - All operators have a precedence level. High precedence level operators are evaluated before lower ones. Operators of the same precedence level are evaluated from left to right TK1913-C Programming19

  20. Example: 2.5 + 6 – 2 * 2 = 2.5 + 6 – 2 * 2 = 4.5 2.5 + 6 – 4 8.5 – 4 4.5 Arithmetic Expression • Example: 2.5 + 6 – 2 * 2 = ?? 2.5 + 6 – 2 * 2 = 2.5 + 6 – 4 8.5 – 4 4.5 TK1913-C Programming20

  21. Try it! • Example: 12 + 6.0 – 2 * 2 = ?? What’s the answer?? TK1913-C Programming21

  22. Arithmetic Expression • All expressions in parentheses (brackets) must be evaluated prior to values outside brackets • Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first • Example: ( 9 – ( 3 + 2 ) ) * 3 = ?? TK1913-C Programming22

  23. Arithmetic Expression • Example: • ( 9 – ( 3 + 2 ) ) * 3 = ??  ( 9 – ( 3 + 2 ) ) * 3 = 12 – 5 12 4 TK1913-C Programming23

  24. Assignment Statement There are 3 types of assignment: • Simple • Multiple • Shorthand TK1913-C Programming24

  25. Don’t forget the semicolon !! Simple Assignment Syntax: variable = expression ; TK1913-C Programming25

  26. Buying price: 10.00 Discount rate: 0.25 For buying price RM10.00 and discount rate 0.25 The total price is RM7.50 _ Buying price: 10.00 Discount rate: _ Buying price: 10.00 Discount rate: 0.25 _ Buying price: 10.00 _ Buying price: 10.00 Discount rate: 0.25 For buying price RM10.00 and discount rate 0.25 _ Buying price: _ price 10.00 ?? ?? discount 0.25 total ?? Simple Assignment • Example: • #include <stdio.h> • void main( ) { • float price, discount, total; • printf(“Buying price : “); • scanf(“%f”, &price); • printf(“\nDiscount rate : “); • scanf(“%f”, &discount); • total = price – (price * discount); • printf(“\nFor buying price RM%.2f and discount rate %.2f\n”, price, discount); • printf(“The total price is RM%.2f\n”, total); • } 7.50 TK1913-C Programming26

  27. Don’t forget the semicolon !! Multiple Assignment Syntax: variable = variable = expression ; TK1913-C Programming27

  28. number ?? 0 ?? 0 total start_x ?? 100.0 start_y ?? 100.0 Multiple Assignment • Example: • int number, total; • float start_x, start_y; • . . . • number = total = 0; • start_x = start_y = 100.0; TK1913-C Programming28

  29. Shorthand Assignment Syntax: variableX = variableX op expression ; variableX op= expression; TK1913-C Programming29

  30. 15 num Shorthand Assignment Whenever the expression on the right contains the variable on the left (to which the value is assigned) • Example: • num = num + 5; 20 15 + 5 20 TK1913-C Programming30

  31. shorthand assignment operator Shorthand Assignment Expressions can also be stated using shorthand assignment operators • Example: • num += 5; similar to num = num + 5 Shorthand assignment operators have the lowest order of precedence – the last one to be evaluated TK1913-C Programming31

  32. Shorthand Assignment TK1913-C Programming32

  33. 100.00 8 5.00 pay hour rate 180.00 Shorthand Assignment • Example: • pay += hour * rate * 2 similar to pay = pay + (hour * rate * 2) • pay + (hour * rate * 2)  pay + (8 * 5.00 * 2)  100.00 + 80.00  180.00 TK1913-C Programming33

  34. Assignment by Value Every assignment expression has a value • Example: • Expression 1: a = 1; • Expression 2: x = y = 0; • Expression 3: p = 12; • p = 0; • Example: • int a, x, y, p; • Line 1: a=1; • Line 2: x = y = 0; • Line 3: p = 12; • Line 4: p = p +3; • Line 5: q = p = p + x; TK1913-C Programming34

  35. Relational Operators TK1913-C Programming35

  36. Mantic/Logical Operators SymbolDescription && AND || OR ! NOT TK1913-C Programming36

  37. a b c d 2 5 15 17 Compound Statement Arithmetic, relational and mantic operators can be integrated/combined in one expression • Example: • ! ( c > a ) • ! ( 15 > 2 )  ! ( 1 )  0 TK1913-C Programming37

  38. a b c d 2 5 15 17 Compound Statement • Example: • (a >= 1) && (b == 5) • ( 2 >= 1 ) && ( b == 5 ) • 1 && ( b == 5 ) • 1 && ( 5 == 5 ) • 1 && 1 • 1 TK1913-C Programming38

  39. d a b c 2 5 15 17 Compound Statement • Example: • (c >= ( b * 3 ) ) || (a == 3) • ( c >= ( 5 * 3 ) ) || ( a == 3) • ( 15 >= 15 ) || ( a == 3) • 1 || ( a == 3 ) • 1 || ( 2 == 3 ) • 1 || 0 • 1 TK1913-C Programming39

  40. d a b c 2 5 15 17 Compound Statement • Example: • ! ( ( a < b ) || ( c > d ) ) • ! ( ( 2 < 5 ) || ( c > d ) ) • ! ( 1 || ( c > d ) ) • ! ( 1 || ( 15 > 17 ) ) • ! ( 1 || 0 ) • ! 1 • 0 TK1913-C Programming40

  41. Increment and Decrement This operation contains only one operand, that is, the operand which value will be incremented/ decremented TK1913-C Programming41

  42. num ?? Increment and Decrement • Example: • int num; • printf(“Key-in a number: “); • scanf(“%d”, &num); • printf(“Value before being incremented: %d\n”, num); • num++; • printf(“Value after being incremented: %d”, num); 26 27 Key-in a number: 26 Value before being incremented: 26 _ Key-in a number: 26 Value before being incremented: 26 Value after being incremented: 27 _ Key-in a number: _ Key-in a number: 26 _ TK1913-C Programming42

  43. Prefix and Postfix Increment and Decrement operators can be either in prefix or postfix forms TK1913-C Programming43

  44. 5 ?? i j Prefix and Postfix • Example: • j = i++ - 2 similar to j = i – 2; i = i + 1; 6 3 TK1913-C Programming44

  45. 5 ?? i j Prefix and Postfix • Example: • j = ++i - 2 similar to i = i + 1; j = i – 2; 6 4 TK1913-C Programming45

  46. 10 x ?? y 10.000000 Coersion Coersion is used to compute a value that is equivalent to its operand’s value (based on the stated data type) • Example: • int x = 10; • float y; • y = (float) x; • ( float ) 10 • 10.000000 TK1913-C Programming46

  47. 15 total 2 number ?? 7.000000 average Coersion • Example: • int total, number; • float average; • … • average = total / number; • 15 / 2 • 7 TK1913-C Programming47

  48. 15 total 2 number ?? 7.500000 average Coersion • Example: • int total, number; • float average; • … • average = (float) total / number; • 15.000000 / 2 • 7.500000 TK1913-C Programming48

  49. Yes !! That’s all? What’s next??? CONTROL STRUCTURE: SELECTION on the way … End of Lecture 6 TK1913-C Programming49

More Related