4. 4 Introduction to Expression Process involves in money withdrawal scenario balance â withdrawed money = current balance Expression in C : bakiTerkini =wangKeluar â bakiSemasa;
5. What is Expression? 5 Introduction to Expression Combination or more than one variable or Constant (operand) which separated by operator Expression Operator example x + 3 - z Operand Consists of arithmetic relational logical
6. 6 Arithmetic Expression Known as Arithmetic Expression Mathematic Expression using Arithmetic Operator Represents by Represents by Unary operator Binary operator
7. Unary Operator 7 Arithmetic Expression Unary Operator Operates for one operand Computer memory cell example -20 a = -20; a b = +15; b +15
8. Unary Operator 8 Arithmetic Expression Example 1: int A = 5; ++A; printf(â%dâ, A); output ? A--; printf(â%dâ,A); output ? A++; printf(â%dâ,A); output ?
10. Unary Operator 10 Arithmetic Expression Example 3: 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;
12. Binary Operator 12 Arithmetic Expression Located between constants or variables or both combination Binary Operator example A+ z operator operand
13. Binary Operator 13 Arithmetic Expression Multiplication Use symbol â * â example A * z operator operand Mathematic Arithmetic Expression 2x + y 2 * x + y
14. Binary Operator 14 Arithmetic Expression Divide Use symbol â/â example A / z operator operand Mathematic Arithmetic Expression 2 : y 2 / y
15. Binary Operator 15 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
16. Binary Operator 16 Arithmetic Expression Example: int A, B; float C; A= 2; B= 5; C= 2.4; B% A; C % A; Valid! Answer is 1 Invalid! C is float
17.
18. Use operator symbol =Assignment statement Double assignment statement Compound assignment statement
19.
20.
21. Example :int a = b= c = d = e = 250; int b =2, number =0, total = 0,average =3; number = b++ = 10; int age = workHour = 0;
27. Arithmetic Operator Precedence Rules 22 Arithmetic Expression Compiler will follows the following precedence to execute the arithmetic expression based on priority.
34. Exercise: 28 Arithmetic Expression Convert the following mathematic expression to a valid arithmetic expression : a) b = 3 + bb) x = (a â b)(a â c2) a + 4 c) d = (3e â d) - ( 4 â 3c3 ) d) r = 2s + 3(s â 9) x â 9 4y s 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)
35. Exercise: 29 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;
36. 30 Relation Expression Relational expression use Relational operator Combination of more than one statement variable vs variable Can consists of variable vs constant produce constant vs constant 0 (if false) 1(if true)
38. 32 Relation Expression P/s: a, b and c are variables, Replace with the given values Example 1: int a=6, b =1, c = -2; a+ b == c 2) a != b 6 + 1== -2 6 != 1 7 == -2 Answer: 0(False) Answer : 1 (True)
39. 33 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)
40. 34 Relation Expression P/s: Relational operator has less priority than other operators. Start evaluating from left to right. Example 3: int a=10, b = 3, c = 7; (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)
41. 35 Relation Expression An example program which uses relational expression #include <stdio.h> void main() { int age; printf(âPlease enter your age >>â); scanf(â%dâ,&age); if (age > 21) printf(âYou are qualified to voteâ); } Relational expression
42. 36 Logical Expression Logical Operator Logical expression use Combination of one or more expressions Can consists of Relational expr. vs logical expr. Relational expr. vs variable produces Relational expr. vs constant 0 (if false) 1(if true)
43. 37 Logical Expression Logical Operator Logical operator && dan || is used between 2 or more relational expression
48. 42 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
49. 43 Logical Expression Logical Expression An example program which using Logical Expression: #include <stdio.h> void main() { int mark; printf(âEnter your mark >>â); scanf(â%dâ,&mark); if (mark >= 85 && mark <= 100) printf(âGred Aâ); else if( mark >=80 && mark <=84) printf(âGred A-â); }
50. Exercise: 44 Logical Expression 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 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);