 # Operators and Expressions

## Presentation on theme: "Operators and Expressions"— Presentation transcript:

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

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

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

A / B Operator and Operand Operator ?? Operand?? Which is which?
Example: A / B Operand?? TK1913-C Programming

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

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

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 / 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

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

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

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

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

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

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

12.0 % 3 = ?? Modulus a float an integer INVALID! Example:
TK1913-C Programming

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

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

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

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

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

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

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

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

Assignment Statement There are 3 types of assignment: Simple Multiple
Shorthand TK1913-C Programming

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

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

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

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

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

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

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

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

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

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

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

Mantic/Logical Operators
Symbol Description && AND || OR ! NOT a b a && b a || b !a !b 1 TK1913-C Programming

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

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

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

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

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

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

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

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

Prefix and Postfix Example: j = ++i - 2 similar to i = i + 1;
5 6 j ?? 4 TK1913-C Programming

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

Coersion int total, number; Example: float average; …
average = total / number; total 15 number 2 average ?? 15 / 2 7 TK1913-C Programming

Coersion int total, number; Example: float average; …
average = (float) total / number; total 15 number 2 average ?? / 2 TK1913-C Programming

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