Download presentation

1
**Operators and Expressions**

Knowledge: Know the types of basic arithmetic operators and their order of precedence Skill: Develop skills in computer arithmetic FTSM 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 Programming

3
**W + Z Operator and Operand Operand Operand Operator**

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

4
**A / B Operator and Operand Operator ?? Operand?? Which is which?**

Example: A / B Operand?? TK1913-C Programming

5
**Basic Arithmetic Operators**

Addition (+) Modulus (%) Subtraction (-) Multiplication (*) Division (/) TK1913-C Programming

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 Programming

7
**W / Z Division Integer Division W and Z are integers Floating Division**

Example: W / Z Integer Division W and Z are integers Floating Division W or Z or both are floats TK1913-C Programming

8
**8 / 2 = 4 Integer Division the result is also an integer an integer**

Example: 8 / 2 = 4 an integer the result is also an integer an integer TK1913-C Programming

9
**12 / 5 = 2 Integer Division the result is also an integer an integer**

Example: 12 / 5 = 2 an integer the result is also an integer an integer TK1913-C Programming

10
**12.0 / 5 = 2 Floating Division a float an integer**

Example: 12.0 / 5 = 2 a float the result is a float an integer TK1913-C Programming

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 Programming

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

13
**12 % 5 = 2 Modulus an integer an integer**

Example: 12 % 5 = 2 an integer the result is the remainder of 12/5 an integer 2 result 5 12 10 remainder 2 TK1913-C Programming

14
**7 % 3 = 1 Modulus an integer an integer**

Example: 7 % 3 = 1 an integer the result is the remainder of 7/3 an integer 2 result 3 7 6 remainder 1 TK1913-C Programming

15
**12.0 % 3 = ?? Modulus a float an integer INVALID! Example:**

TK1913-C Programming

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 Programming

17
**Arithmetic Expression**

An expression may contain 2 or more arithmetic operators Main issue: ORDER OF PRECEDENCE TK1913-C Programming

18
**Arithmetic Expression**

Examples: 5 + 6 = 11 5 + 6 * 2 = 17 = 22 or 17? – 2 * 2 = ?? = ?? 12 / 6.0 – 2 * 2 TK1913-C Programming

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 Programming

20
**Arithmetic Expression**

Example: Example: – 2 * 2 = 4.5 4 8.5 – – 4 4.5 – 2 * 2 = – 2 * 2 = ?? – 2 * 2 = – 4 8.5 – 4 4.5 TK1913-C Programming

21
**Try it! 12 + 6.0 – 2 * 2 = ?? What’s the answer?? Example:**

TK1913-C Programming

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 = ?? TK1913-C Programming

23
**Arithmetic Expression**

Example: ( 9 – ( ) ) * 3 = ?? ( 9 – ( ) ) * 3 = 12 – 12 4 TK1913-C Programming

24
**Assignment Statement There are 3 types of assignment: Simple Multiple**

Shorthand TK1913-C Programming

25
**variable = expression ;**

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

26
**Simple Assignment 0.25 10.00 ?? 7.50 Example: price discount total**

Buying price: 10.00 Discount rate: 0.25 For buying price RM10.00 and discount rate 0.25 _ 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: 0.25 _ 0.25 Buying price: 10.00 Discount rate: _ Buying price: 10.00 _ 10.00 Buying price: _ 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); } discount ?? price total 7.50 TK1913-C Programming

27
**variable = variable = expression ;**

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

28
**Multiple Assignment Example: int number, total;**

float start_x, start_y; . . . number = total = 0; start_x = start_y = 100.0; total ?? number start_x ?? start_y 100.0 TK1913-C Programming

29
**Shorthand Assignment variableX = variableX op expression ;**

Syntax: variableX = variableX op expression ; variableX op= expression; TK1913-C Programming

30
**Shorthand Assignment Example: 15 + 5 20 num = num + 5;**

Whenever the expression on the right contains the variable on the left (to which the value is assigned) Example: num = num + 5; num 15 20 15 + 5 20 TK1913-C Programming

31
**Shorthand Assignment Example: similar to num = num + 5 num += 5;**

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

32
**Examples of expression**

Shorthand Assignment Operation Examples of expression Description += num += 5; num = num + 5; -= num -= 5; num = num – 5; *= num *= 5; num = num * 5; /= num /= 5; num = num / 5; %= num %= 5; num = num % 5; TK1913-C Programming

33
**Shorthand Assignment Example: pay += hour * rate * 2**

similar to pay = pay + (hour * rate * 2) 180.00 pay 100.00 hour 8 rate 5.00 pay + (hour * rate * 2) pay + (8 * 5.00 * 2) TK1913-C Programming

34
**Assignment by Value Every assignment expression has a value**

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; Example: Expression 1: a = 1; Expression 2: x = y = 0; Expression 3: p = 12; p = 0; TK1913-C Programming

35
**Examples of Expression**

Relational Operators Operation Description Examples of Expression Value < Less than 6 < 9 1 (true) <= Less than or equal to 5 <= 5 > Greater than 2 > 6 0 (false) >= Greater than or equal to 9 >= 5 == Equal to 7 == 5 != Not equal to 6 != 5 TK1913-C Programming

36
**Mantic/Logical Operators**

Symbol Description && AND || OR ! NOT a b a && b a || b !a !b 1 TK1913-C Programming

37
**Compound Statement ! ( c > a ) Example:**

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

38
**Compound Statement (a >= 1) && (b == 5) Example:**

2 1 && ( b == 5 ) b 5 1 && ( 5 == 5 ) c 15 1 && 1 d 17 1 TK1913-C Programming

39
**Compound Statement (c >= ( b * 3 ) ) || (a == 3) Example:**

( c >= ( 5 * 3 ) ) || ( a == 3) a 2 ( 15 >= 15 ) || ( a == 3) b 5 1 || ( a == 3 ) 1 || ( 2 == 3 ) c 15 1 || 0 d 17 1 TK1913-C Programming

40
**Compound Statement ! ( ( a < b ) || ( c > d ) ) Example:**

2 ! ( 1 || ( c > d ) ) b 5 ! ( 1 || ( 15 > 17 ) ) ! ( 1 || 0 ) c 15 ! 1 d 17 TK1913-C Programming

41
**Increment and Decrement**

This operation contains only one operand, that is, the operand which value will be incremented/ decremented Symbol Description Examples of Expression ++ Increment operand by 1 i++ i = i + 1 -- Decrement operand by 1 i-- i = i – 1 TK1913-C Programming

42
**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); num ?? 26 27 Key-in a number: 26 Value before being incremented: 26 Value after being incremented: 27 _ Key-in a number: 26 Value before being incremented: 26 _ Key-in a number: _ Key-in a number: 26 _ TK1913-C Programming

43
Prefix and Postfix Increment and Decrement operators can be either in prefix or postfix forms Expression Description i++ Value of i is incremented after being used in the expression ++i Value of i is incremented before being used in the expression i-- Value of i is decremented after being used in the expression --i Value of i is decremented before being used in the expression TK1913-C Programming

44
**Prefix and Postfix Example: j = i++ - 2 similar to j = i – 2;**

i = i + 1; i 5 6 j ?? 3 TK1913-C Programming

45
**Prefix and Postfix Example: j = ++i - 2 similar to i = i + 1;**

5 6 j ?? 4 TK1913-C Programming

46
**Coersion int x = 10; Example:**

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; x 10 y ?? ( float ) 10 TK1913-C Programming

47
**Coersion int total, number; Example: float average; …**

average = total / number; total 15 number 2 average ?? 15 / 2 7 TK1913-C Programming

48
**Coersion int total, number; Example: float average; …**

average = (float) total / number; total 15 number 2 average ?? / 2 TK1913-C Programming

49
**End of Lecture 6 Yes !! That’s all? What’s next???**

CONTROL STRUCTURE: SELECTION on the way … TK1913-C Programming

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google