Chapter 4 Expression & Operators 1
Aim To give understanding on: Expression and operator concept math.h and stdlib.h built-in function Objective Students should be able to: understand concepts and fundamentals in expression/ operator. write expression in C programming language 2
Introduction to Expression Given the following statement : 2x + 3 z = y expression 3
Introduction to Expression Process involves in money withdrawal scenario balance withdrawed money = current balance Expression in C : bakiterkini =wangkeluar bakisemasa; 4
What is Expression? Expression Introduction to Expression Combination or more than one variable or Constant (operand) which separated by operator example x + 3 - z Operator Operand Consists of arithmetic relational logical 5
Arithmetic Expression Arithmetic Expression Known as Mathematic Expression using Arithmetic Operator Represents by Unary operator Binary operator Represents by Operator Meaning - negative + positive -- decrement ++ increment Operator Meaning * multiply / divide + add - subtract % modulus 6
Unary Operator Arithmetic Expression Unary Operator example Operates for one operand Computer memory cell a = -20; a -20 b = +15; b +15 Increment Decrement prefix ++c; --c; postfix c++; c--; 7
Unary Operator Example 1: int A = 5; ++A; printf( %d, A); output? A--; printf( %d,a); output? A++; Arithmetic Expression printf( %d,a); output? 8
Unary Operator Example 2: Arithmetic Expression int A ; A=6; printf( %d,3 + --A); output? printf( %d, A); output? A=6; printf( %d, 3 + A--); output? printf( %d, A); output? 9
Unary Operator Example 3: Arithmetic Expression Given the value of num1 = 8.Determine the value of num2 after the execution for each of the following statements: num2 = num1++ - 2; num2 = num1; num2 = ++num1 3; num2 = num1-- +1; 10
11
Binary Operator Arithmetic Expression Binary Operator Located between constants or variables or both combination example A + z operator operand 12
Binary Operator Arithmetic Expression Multiplication Use symbol * example A * z Mathematic 2x + y operator operand Arithmetic Expression 2 * x + y 13
Binary Operator Arithmetic Expression Divide Use symbol / example A / z operator operand Mathematic 2 : y Arithmetic Expression 2 / y 14
Binary Operator Arithmetic Expression Modulus Use symbol % example A % z operator operand Return a balance when 2 numbers is divided Can only be used with an integer variable 15
Binary Operator Example: int A, B; float C; A= 2; B= 5; Arithmetic Expression C= 2.4; B% A; C % A; Valid! Answer is 1 Invalid! C is float 16
Arithmetic Expression Assignment Statement Used to store value/result of process to a variable Use operator symbol = Assignment statement Double assignment statement Compound assignment statement 17
Arithmetic Expression Assignment Statement Format /sintax : variable = value; variable = constant; or variable = variable; variable = expression; Example : 1.average= ( 6 + 5) * 4; 2.grossSalary = 1500; nettsalary = grosssalary + 200; 3.price= 50.00; pay = price; ` average 44 grosssalary 1500 nettsalary 1700 price pay 50.00 50.00 18
Arithmetic Expression Compound Assignment Statement Use more than one operator (=) Example : int a = b= c = d = e = 250; int b =2, number =0, total = 0,average =3; number = b++ = 10; int age = workhour = 0; 19
Function Arithmetic Expression Compound Assignment Statement To combine two different operator together. To simplify arithmetic operator Original function of operator does not affected Allowed combination are as follow: operator: +=, -=, *=, /=, %= 20
Compound Assignment Statement Example : Arithmetic Expression Assignment Expression Meaning Operator += total + = 300 total = total+ 300 -= total - = count+ 300 total = total - (count + 300) *= total *=300 total = total * 300 /= total /= count 10 total = total / ( count 10) %= total % = 7 total = total % 7 21
Arithmetic Expression Arithmetic Operator Precedence Rules Compiler will follows the following precedence to execute the arithmetic expression based on priority. Operator Arrangement/Priority (if appears more than once in a statement) ( ) Left to right ++, -- Right to left *, /, % Left to right +, - Left to right 22
Arithmetic Operator Arithmetic Operator Precedence Rules Example: 1. 5 + 2 * 6 4 / 2 2. 3 * 4 / 2 + ( 3 1) 5 + 12-4 / 2 3 * 4 / 2 + 2 5 + 12-2 12 / 2 + 2 17-2 6 + 2 15 8 23
Arithmetic Operator Precedence Rules Example: Arithmetic Expression 3. Prefix unary arithmetic expression int kira = 5, nilai pertama = 10; nilai_kedua = 5 * --kira + nilai_pertama; printf( %d %d, kira, nilai_kedua); Output: 4 30 24
Arithmetic Operator Precedence Rules Example: Arithmetic Expression 3. Prefix unary arithmetic expression int kira = 5, nilai pertama = 10; nilai_kedua = 5 * kira-- + nilai_pertama; printf( %d %d, kira, nilai_kedua); Output: 4 35 25
Arithmetic Expression Mathematic Library Function Diwakili oleh perpustakaan piawai matematk iaitu math.h Dipanggil bersama #include Antara fungsi perpustakaan matematik yang penting: Fungsi Tujuan sqrt(x) Kembalikan nilai puncagandadua x di mana x >= 0 pow(x) Kembalikan x ganda y cos(x) Kembalikan cos x di mana x di dalam radian sin(x) Kembalikan sin x di mana x di dalam radian tan(x) Kembalikan tan x di mana x di dalam radian 26
Example: Arithmetic Expression Mathematic Library Function #include<stdio.h> #include <math.h> void main() { int x = 16, y ; y = sqrt(x); printf( %d,y); Output : 4 } 27
Exercise: Arithmetic Expression 1. Convert the following mathematic expression to a valid arithmetic expression : a) b = 3 + b b) x = (a b)(a c 2 ) a + 4 c) d = (3e d) - ( 4 3c 3 ) d) r = 2s + 3(s 9) x 9 4y s 2. Given a= 3, b = 5, c=1. What is the output of the following expression? a. ( 6 * c 6 / a) - b b. (5 * c) +( a* b / b) c. ++a d. c + a * c / (3 * c) 28
Exercise: Arithmetic Expression 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: a) k = j++; d) k = ++j; b) k = i * j--; e) k = i * --j; c) k = j + i * j++; f) k = 27 / j++ - 16 % i; 29
Relation Expression Relational expression use Relational operator Combination of more than one statement Can consists of variable vs variable produce variable vs constant constant vs constant 0 (if false) 1(if true) 30
Relation Expression Relational Operator Operator Description = = Equal to > Greater than < Less than >= Greater than or equal <= Less than or equal!= Not equal 31
Relation Expression Example 1: P/s: a, b and c are variables, Replace with the given values int a=6, b =1, c = -2; 1) a+ b == c 2) a!= b 6 + 1== -2 6!= 1 7 == -2 Answer: 0(False) Answer : 1 (True) 32
Relation Expression Example 2 : int a=6, b =1, c = -2; 3) b < a 4) b + c <= a 1 < -2 1 + -2 < 6-1 < 6 Answer: 0 (False) Answer : 1 (True) 33
Relation Expression Example 3: int a=10, b = 3, c = 7; P/s: Relational operator has less priority than other operators. Start evaluating from left to right. (a+b >= 3*c)==( a!= 2*c+b) (10+3 >= 3*7)==(a!= 2*c+b) (13 >= 21)==(10!= 14+3) (13 >= 21)==(10!= 17) 0 == 1 0 (false) 34
Relation Expression An example program which uses relational expression #include <stdio.h> void main() { int age; printf( \nplease enter your age >> ); scanf( %d,&age); if (age > 21) Relational expression printf( \nyou are qualified to vote ); } 35
Logical Expression Logical expression use Logical Operator Combination of one or more expressions Can consists of Relational expr. vs logical expr. produces Relational expr. vs variable Relational expr. vs constant 0 (if false) 1(if true) 36
Logical Expression Logical Operator Operator && Description AND OR! NOT Logical operator && dan is used between 2 or more relational expression 37
Logical Expression Logical operator truth table for AND AND (&&) Logical Operator Result False AND False False Value 0 1 False AND True False 0 0 0 True AND False False 1 0 1 True AND True True 38
Logical Expression Logical operator truth table for OR OR ( ) Logical Operator Result False OR False False Value 0 1 False OR True True 0 0 1 True OR False Trrue 1 1 1 True OR True True 39
Logical Expression Logical operator truth table for NOT NOT(!) Value Result!0 1!1 0 Logical Operator Not false Not true Result True False 40
Logical Expression Example 1: Evaluate the following logical expression: a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) ( 2 * 3 == 6) 1 && 1 (1 < 2) (6 == 6) 1 1 1 1 41
Logical Expression Example 2: Evaluate the following logical expression: Given a = 3, b = 4; c)!((5 * b <= 23 - a )) d)! ((b +3!= 8) &&( 3 * a < 2))!((5 * 4 <= 23 3))!(( 7!= 8 ) && ( 9 < 2 ))!(20 <= 20)! ( 1 && 0 )!(1)! ( 0) 0 1 42
Logical Expression An example program which using Logical Expression: #include <stdio.h> void main() { int mark; printf( \nenter your mark >> ); scanf( %d,&mark); if (mark >= 85 && mark <= 100) printf( \ngred A ); } else if( mark >=80 && mark <=84) printf( \ngred A- ); 43
Logical Expression Exercise: 1. Given with i=2, j = 5 and k = 15. Evaluate each of the following expression: a) i > j k g) k == j + I * j b) i!= k h) k <=k /j c) (i >= 1) && (j == 5) i) (j < i) (k > j) d)!( i > j) j) (i >0) && (j <k) (k <i) e) i < j < k k) i * k < k / j f) (i < j) && (j < k) i) i j > k 2. Complete the following statements with suitable logical expression. int angka1,angka2; if (angka1 is less than or equal to angka2) printf( %d is less than or equal to %d,angka1,angka2) 44