chapter3dti ok.pptx

44
DTI2143 Computer Programming 1 Chapter 3 Expression & Operators

Upload: hashemyahyaqalmahdi

Post on 06-Dec-2015

227 views

Category:

Documents


3 download

DESCRIPTION

chapter3dti ok.pptx

TRANSCRIPT

Page 1: chapter3dti ok.pptx

DTI2143 Computer Programming

1

Chapter 3Expression

&Operators

Page 2: chapter3dti ok.pptx

Aim

2

To give understanding on: Expression and operator concept math.h and stdlib.h built-in function

Students should be able to: understand concepts and fundamentals in expression/ operator. write expression in C programming language

Objective

Page 3: chapter3dti ok.pptx

3

Given the following statement :

2x + 3 – z = y

expression

Introduction to Expression

Page 4: chapter3dti ok.pptx

4

Process involves in money withdrawal scenario

balance – withdrawed money = current balance

Introduction to Expression

Expression in C :

bakiTerkini =wangKeluar – bakiSemasa;

Page 5: chapter3dti ok.pptx

What is Expression?

5

ExpressionCombination or more than one variable or Constant (operand) which separated by operator

x + 3 - z

Operand

Operatorexample

Consists of

arithmetic

relational

logical

Introduction to Expression

Page 6: chapter3dti ok.pptx

6

Arithmetic Expression Mathematic Expression

Known as

usingArithmetic Operator

Unary operator Binary operatorRepresents by

Operator Meaning- negative

+ positive

-- decrement

++ increment

Represents by

Operator Meaning* multiply

/ divide

+ add

- subtract

% modulus

Arithmetic Expression

Page 7: chapter3dti ok.pptx

Unary Operator

7

Unary Operator Operates for one operand

example

a = -20; a -20

Computer memory cell

b = +15; b +15

Increment Decrement

prefix ++c; --c;

postfix c++; c--;

Arithmetic Expression

Page 8: chapter3dti ok.pptx

Unary Operator

8

Example 1:

int A = 5;

++A;

printf(“%d”, A); output ?A--;

printf(“%d”,A); output ?A++;

printf(“%d”,A); output ?

Arithmetic Expression

Page 9: chapter3dti ok.pptx

Unary Operator

9

Example 2:

int A ;

A=6;

printf(“%d”,3 + --A); output ?

printf(“%d”, A); output ?

A=6;

printf(“%d”, 3 + A--); output ?

printf(“%d”, A); output ?

Arithmetic Expression

Page 10: chapter3dti ok.pptx

Unary Operator

10

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;

Arithmetic Expression

Page 11: chapter3dti ok.pptx

11

Page 12: chapter3dti ok.pptx

Binary Operator

12

Binary Operator Located between constants or variables or both combination

exampleA + z

operatoroperand

Arithmetic Expression

Page 13: chapter3dti ok.pptx

Binary Operator

13

Multiplication Use symbol “ * ”

exampleA * z

operatoroperand

Mathematic

2x + y

Arithmetic Expression

2 * x + y

Arithmetic Expression

Page 14: chapter3dti ok.pptx

Binary Operator

14

Divide Use symbol “/”

example A / z

operatoroperand

Mathematic

2 : y

Arithmetic Expression

2 / y

Arithmetic Expression

Page 15: chapter3dti ok.pptx

Binary Operator

15

Modulus Use symbol “%”

example A % z

operator

operandReturn a balance when 2 numbers is divided

Can only be used with an integer variable

Arithmetic Expression

Page 16: chapter3dti ok.pptx

Binary Operator

16

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

Arithmetic Expression

Page 17: chapter3dti ok.pptx

Assignment Statement

17

Used to store value/result of process to a variable Use operator symbol =

Assignment statement

Double assignmentstatement

Compound assignmentstatement

Arithmetic Expression

Page 18: chapter3dti ok.pptx

Assignment Statement

18

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;`

44average

grossSalary

1500

nettSalary 1700

price

pay

50.00

50.00

Arithmetic Expression

Page 19: chapter3dti ok.pptx

Compound Assignment Statement

19

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;

Arithmetic Expression

Page 20: chapter3dti ok.pptx

Compound Assignment Statement

20

To combine two different operator together. To simplify arithmetic operator Original function of operator does not

affected Allowed combination are as follow:

Arithmetic Expression

+= , -= , *= , /= , %=

Function

operator:

Page 21: chapter3dti ok.pptx

Compound Assignment Statement

21

Example :

Assignment

Operator

Expression Meaning

+= 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

Arithmetic Expression

Page 22: chapter3dti ok.pptx

Arithmetic Operator Precedence Rules

22

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

Arithmetic Expression

Page 23: chapter3dti ok.pptx

Arithmetic Operator Precedence Rules

23

Example:

1. 5 + 2 * 6 – 4 / 2

5 + 12 - 4 / 2

5 + 12 - 2

17 - 2

15

2. 3 * 4 / 2 + ( 3 –1)

3 * 4 / 2 + 2

12 / 2 + 2

6 + 2

8

Arithmetic Operator

Page 24: chapter3dti ok.pptx

Arithmetic Operator Precedence Rules

24

Example:

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

Arithmetic Expression

Page 25: chapter3dti ok.pptx

Arithmetic Operator Precedence Rules

25

Example:

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

Arithmetic Expression

Page 26: chapter3dti ok.pptx

26

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

Arithmetic Expression

Page 27: chapter3dti ok.pptx

27

Mathematic Library FunctionExample:

#include<stdio.h>#include <math.h>

void main(){ int x = 16, y ; y = sqrt(x); printf(“%d”,y);

}

Output :

4

Arithmetic Expression

Page 28: chapter3dti ok.pptx

Exercise:

28

1. Convert the following mathematic expression to a valid arithmetic expression :

a) b = 3 + b b) x = (a – b)(a – c2) a + 4

c) d = (3e – d) - ( 4 – 3c3 ) 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)

Arithmetic Expression

Page 29: chapter3dti ok.pptx

Exercise:

29

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;

Arithmetic Expression

Page 30: chapter3dti ok.pptx

30

Relationalexpression

use Relational operator

Combination of more than one statement

variable vs variable

variable vs constant

constant vs constant

Can consists of

produce

0 (if false) 1(if true)

Relation Expression

Page 31: chapter3dti ok.pptx

Relational Operator

31

Operator Description

= = Equal to

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

!= Not equal

Relation Expression

Page 32: chapter3dti ok.pptx

32

int a=6, b =1, c = -2;

1) a+ b == c 2) a != b

6 + 1== -2 6 != 17 == -2

Answer: 0(False) Answer : 1 (True)

P/s:a, b and c are variables,Replace with the given values

Example 1:

Relation Expression

Page 33: chapter3dti ok.pptx

33

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)

Example 2 :

Relation Expression

Page 34: chapter3dti ok.pptx

34

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 == 10 (false)

P/s:Relational operator has less priority than other operators.Start evaluating from left to right.

Example 3:

Relation Expression

Page 35: chapter3dti ok.pptx

35

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)

printf(“\nYou are qualified to vote”);

}

Relational expression

Relation Expression

Page 36: chapter3dti ok.pptx

36

Logical expressionuse

Logical Operator

Combination of one or more expressions

Relational expr. vs logical expr.

Relational expr. vs variable

Relational expr. vs constant

Can consists of

produces

0 (if false) 1(if true)

Logical Expression

Page 37: chapter3dti ok.pptx

37

Logical Operator

Operator Description

&& AND

|| OR

! NOT

Logical operator && dan || is used between 2 or morerelational expression

Logical Expression

Page 38: chapter3dti ok.pptx

38

Logical operator truth table for AND

Value 0 1

0 0 0

1 0 1

AND (&&) Logical Operator Result

False AND False False

False AND True False

True AND False False

True AND True True

Logical Expression

Page 39: chapter3dti ok.pptx

39

Logical operator truth table for OR

Value 0 1

0 0 1

1 1 1

OR (||) Logical Operator

Result

False OR False False

False OR True True

True OR False Trrue

True OR True True

Logical Expression

Page 40: chapter3dti ok.pptx

40

Value Result

!0 1

!1 0

NOT(!)

Logical Operator

Result

Not false True

Not true False

Logical operator truth table for NOT

Logical Expression

Page 41: chapter3dti ok.pptx

41

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

Logical Expression

Page 42: chapter3dti ok.pptx

42

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

Logical Expression

Page 43: chapter3dti ok.pptx

43

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-”);

}

Logical ExpressionLogical Expression

Page 44: chapter3dti ok.pptx

Exercise:

44

1. Given with i=2, j = 5 and k = 15. Evaluate each of the following expression:a) i > j – k g) k == j + I * jb) i != k h) k <=k /jc) (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 / jf) (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);

Logical Expression