Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bab 4: Operator & Ungkapan

Similar presentations


Presentation on theme: "Bab 4: Operator & Ungkapan"— Presentation transcript:

1 Bab 4: Operator & Ungkapan

2 Pengenalan Perhatikan ungkapan berikut: y = x + 3 a > b
a, b, x, y dan 3 adalah operan (terdiri daripada pemalar dan pembolehubah) Simbol =, + dan > adalah operator

3 Operator (C) Operator Umpukan Operator Aritmetik Operator Hubungan
Operator Logik

4 Operator Umpukan Simbol = Contoh: x = y; Umumnya:
Penggunaan berbeza dengan rumus matematik Contoh: x = y; Untuk aturcara C, bermaksud ‘umpukkan nilai y kepada x’ Umumnya: pembolehubah = ungkapan; Jum = bil * harga;

5 Operator Aritmetik Operator Unari
Bertindak/beroperasi terhadap 1 operan Simbol Contoh Nota + +2 No. +ve; lalai (default) - -30 No. -ve ++ ++nilai nilai++ Prefix:sblm pelaksanaan Postfix:slps pelaksanaan -- --bil bil--

6 CONTOH PENGGUNAAN OPERATOR TAMBAH
POSTINCREMENT PREINCREMENT Int count = 5; : printf(“%d”, count++ ); printf(“%d”, count ); hasil ialah 5 6 Int count = 5; : printf(“%d”, ++count ); printf(“%d”, count ); hasil ialah 6 6

7 CONTOH PENGGUNAAN OPERATOR TOLAK
POSTDECREMENT PREDECREMENT Kira = 5; a = 10; : b = 5 * --kira + a; printf(“%d %d”, kira, b); hasil ialah b = * --kira + a; └─┬─┘ (a) 4 └──┬──┘ (b) 20 └───┬──┘ (c) 30 kira = 5; a = 10; : b = 5 * kira-- + a; printf(“%d %d”, kira, b); hasil ialah b = * kira a; └─┬─┘ (a) 5 └──┬──┘ (b) 25 └───┬──┘ (c) 35

8 Operator Aritmetik Operator Binari Diletakkan di antara 2 operan
Symbol Action Example Addition + Adds two operands x + y Subtraction - Subtracts the second operand from the first operand x - y Multiplication * Multiplies two operands x * y Division / Divides the first operand by the second operand x / y Modulus % Gives the remainder when the first operand is divided by the second operand (integers only) x % y

9 Operator Aritmetik Operator Binari – isu: Ungkapan kompleks
Contoh: a * b + c a * ( b + c) Jenis data berbeza int a, b, c; float d; c = a/b;  apakah hasilnya? d = a/b;  apakah hasilnya?

10 Operator Aritmetik Keutamaan Contoh: 1 ( ) 2 * , / , % 3 + , -
Jumlah1 = i + j * l / k; Keutamaan Operasi 1 ( ) 2 * , / , % 3 + , -

11 CONTOH OPERASI ARITMETIK
* 6 – 4 / 2 └─┬─┘ – 4 / 2 └─┬┘ – 2 17 – 2 15

12 Operator Aritmetik Casting
Diperlukan untuk menukarkan jenis data dalam storan sementara Menggunakan operator cast Contoh: double a,b,c; c = a % b; //ralat! c = (int) a % (int) b; Cast Operator

13 CONTOH PENGGUNAAN CASTING
double nilai1 = 9.5, nilai3; int nilai2; : nilai2 = nilai1 % 3; nilai3 = ( nilai1 * 10 % 20 ) / 2.0 nilai2 = ( int ) nilai1 % 3; nilai3 = (( int ) ( nilai1 * 10 ) % 20 ) / 2.0; TIDAK SAH

14 Fungsi Pustaka Matematik
Diperlukan untuk masalah libat rumus matematik yang lebih rumit, contoh: Fail kepala yang diperlukan: math.h ceil(x), floor(x), abs(x), fabs(x), sqrt(x), pow(x,y), cos(x), sin(x), tan(x), exp(x), log(x), log10(x)

15 Fungsi Pustaka Matematik
Contoh penggunaan: Arahan Rumus matematik A = pow(6,100); A = 6100 B = pow(x,3); B = x3 (atau arahan C: B = x * x * x; ) C = sqrt(144);

16 Operator Hubungan Untuk melakukan perbandingan, contoh: ‘Adakah A lebih besar drpd B?’, ‘Adakah Y bernilai sifar?’, dsb. Nilai yang dipulangkan sama ada 1 (‘ya’/benar/TRUE) atau 0 (‘tidak’/palsu/FALSE)

17 Operator Hubungan Operator Symbol Question Asked Example Equal ==
Is operand1 equal to operand2? x == y Greater than > Is operand1 greater than operand2? x > y Less than < Is operand1 less than operand2? x < y Greater than or equal to >= Is operand1 greater than or equal to operand2? x >= y Less than or equal to <= Is operand1 less than or equal to operand2? x <= y Not equal != Is operand1 not equal to operand2? x != y

18 Operator Hubungan Contoh: Expression How It Reads What It Evaluates To
5 == 1 Is 5 equal to 1? 0 (false) 5 > 1 Is 5 greater than 1? 1 (true) 5 != 1 Is 5 not equal to 1? (5+10) == (3*5) Is (5+10) equal to (3*5)?

19 Operator Logik  0 < Y < 10
Digunakan utk menggabungkan beberapa ungkapan yg mengandungi operator hubungan Berguna utk membuat perbandingan kompleks bg membuat keputusan Contoh: ‘Adakah Y lebih besar daripada 0 dan Y lebih kecil daripada 10?’  0 < Y < 10

20 Operator Logik Nilai yang dipulangkan: 1 (benar), 0 (palsu) Contoh:
Symbol Example AND && exp1 && exp2 OR || exp1 || exp2 NOT ! !exp1

21 Operator Logik Menilai ungkapan untuk operator &&: Ungkapan1 Ungkapan2
Hasil 1

22 Operator Logik Menilai ungkapan untuk operator ||: Ungkapan1 Ungkapan2
Hasil 1

23 Operator Logik Menilai ungkapan untuk operator !: Nilai Sebenar
Gabungan Operator ! Hasil 1 !(1) !(0)

24 Operator Logik Contoh penggunaan: Expression What It Evaluates To
(5 == 5) && (6 != 2) True (1), because both operands are true (5 > 1) || (6 < 1) True (1), because one operand is true (2 == 1) && (5 == 5) False (0), because one operand is false !(5 == 4) True (1), because the operand is false

25 PERLAKSANAAN MENGIKUT KEUTAMAAN
( ) ! +(unary) -(unary) * / % + - < <= > >= == != && ||

26 LATIHAN int a = 10, b = 3, c = 7; ( a + b >= 3 * c ) == ( a != 2 * c + b)

27 Operator sizeof( ) Saiz ruang ingatan yang diperlukan untuk menyimpan ungkapan tertentu dalam unit byte. Contoh Int x sizeof(x); Saiz bagi integer -> *2 byte Saiz bagi char  *1 byte * mengikut jenis komputer

28 Compound expression vs simple expression
x *= y x /= y x %= y x + = y ? x /= y-1 x = x * y x = x / y x = x % y x = x * (y+3)


Download ppt "Bab 4: Operator & Ungkapan"

Similar presentations


Ads by Google