Bab 4 Operator Ungkapan Pengenalan n Perhatikan ungkapan
Bab 4: Operator & Ungkapan
Pengenalan n 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
Operator (C) n n Operator Umpukan Aritmetik Hubungan Logik
Operator Umpukan n Simbol = n n Contoh: x = y; n n Penggunaan berbeza dengan rumus matematik Untuk aturcara C, bermaksud ‘umpukkan nilai y kepada x’ Umumnya: pembolehubah = ungkapan; Jum = bil * harga;
Operator Aritmetik n Operator Unari n Bertindak/beroperasi terhadap 1 operan Simbol Contoh Nota + ++ +2 -30 ++nilai++ No. +ve; lalai (default) No. -ve Prefix: sblm pelaksanaan Postfix: slps pelaksanaan -- --bil bil-- Prefix: sblm pelaksanaan Postfix: slps pelaksanaan
CONTOH PENGGUNAAN OPERATOR TAMBAH POSTINCREMENT PREINCREMENT Int count = 5; : printf(“%d”, count++ ); printf(“%d”, count ); Int count = 5; : printf(“%d”, ++count ); printf(“%d”, count ); hasil ialah 5 6 hasil ialah 6 6
CONTOH PENGGUNAAN OPERATOR TOLAK POSTDECREMENT PREDECREMENT Kira = 5; a = 10; : b = 5 * --kira + a; printf(“%d %d”, kira, b); kira = 5; a = 10; : b = 5 * kira-- + a; printf(“%d %d”, kira, b); hasil ialah 4 30 hasil ialah 4 35 b= b= 5 * --kira + a; └─┬─┘ (a) 4 └──┬──┘ (b) 20 └───┬──┘ (c) 30 5 * kira-- + a; └─┬─┘ (a) 5 └──┬──┘ (b) 25 └───┬──┘ (c) 35
Operator Aritmetik n Operator Binari n Diletakkan di antara 2 operan Operato Symb r ol Addition + Subtracti on Multiplic ation Division Modulus - Action Adds two operands Exam ple x+y * Subtracts the second operand from the first operand Multiplies two operands x-y x*y / Divides the first operand by the second operand x/y % Gives the remainder when the first operand is divided by the second operand (integers only) x%y
Operator Aritmetik n Operator Binari – isu: n Ungkapan kompleks n n Contoh: a * b + c a * ( b + c) Jenis data berbeza n Contoh: int a, b, c; c = a/b; d = a/b; float d; apakah hasilnya?
Operator Aritmetik n n Keutamaan Operasi 1 () 2 *, /, % 3 +, - Contoh: Jumlah 1 = i + j * l / k;
Operator Aritmetik n Casting n n n 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
CONTOH PENGGUNAAN CASTING double nilai 1 = 9. 5, nilai 3; int nilai 2; : nilai 2 = nilai 1 % 3; TIDAK SAH nilai 3 = ( nilai 1 * 10 % 20 ) / 2. 0 nilai 2 = ( int ) nilai 1 % 3; nilai 3 = (( int ) ( nilai 1 * 10 ) % 20 ) / 2. 0;
Fungsi Pustaka Matematik n n 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 10(x)
Fungsi Pustaka Matematik n Contoh penggunaan: Arahan Rumus matematik A = pow(6, 100); A = 6100 B = pow(x, 3); B = x 3 (atau arahan C: B = x * x; ) C = sqrt(144);
Operator Hubungan n n 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)
Operator Hubungan Operator Question Asked Equal Symb ol == Greater than > Less than < Is operand 1 greater than operand 2? Is operand 1 less than operand 2? Greater than or equal to Less than or equal to Not equal >= <= != Is operand 1 equal to operand 2? Exam ple x == y x>y x<y Is operand 1 greater than or equal x >= y to operand 2? Is operand 1 less than or equal to x <= y operand 2? Is operand 1 not equal to x != y operand 2?
Operator Hubungan n 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? 1 (true) (5+10) == (3*5) Is (5+10) equal to (3*5)? 1 (true)
Operator Logik n n n 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
Operator Logik n n Nilai yang dipulangkan: 1 (benar), 0 (palsu) Contoh: Operator Symbol Example AND && exp 1 && exp 2 OR || exp 1 || exp 2 NOT ! !exp 1
Operator Logik n Menilai ungkapan untuk operator &&: Ungkapan 1 Ungkapan 2 Hasil 1 1 0 0 0 1 0 0
Operator Logik n Menilai ungkapan untuk operator ||: Ungkapan 1 Ungkapan 2 Hasil 1 1 0 0 0
Operator Logik n Menilai ungkapan untuk operator !: Nilai Sebenar Ungkapan 1 Gabungan Operator ! Hasil !(1) 0 0 !(0) 1
Operator Logik n 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
PERLAKSANAAN MENGIKUT KEUTAMAAN () ! +(unary) -(unary) ++ -- * / % + < <= > >= == != && ||
LATIHAN int a = 10, b = 3, c = 7; ( a + b >= 3 * c ) == ( a != 2 * c + b)
Operator sizeof( ) n n Saiz ruang ingatan yang diperlukan untuk menyimpan ungkapan tertentu dalam unit byte. Contoh n n n Int x sizeof(x); Saiz bagi integer -> *2 byte Saiz bagi char *1 byte * mengikut jenis komputer
Compound expression vs simple expression Compound expression Simple expression x x x x ? x ? *= y /= y %= y +=y /= y-1 =x*y =x/y =x%y = x * (y+3)
- Slides: 28