0% found this document useful (0 votes)
172 views

Bab 4-Expression and Operators

This document discusses various types of operators in computer programming including arithmetic, relational, logical, and assignment operators. It provides examples of how each operator is used and the functions they perform. The key points are: - Operators are symbols used to manipulate values and variables and are divided into arithmetic, relational, and logical categories. - Arithmetic operators include addition, subtraction, multiplication, division, and modulo and are used to perform math operations on numeric data types. - Relational operators compare values and include less than, greater than, equal to, etc. - Logical operators combine conditional statements and include AND, OR, and NOT functions. - Assignment operators assign values to variables like

Uploaded by

Rohaiza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

Bab 4-Expression and Operators

This document discusses various types of operators in computer programming including arithmetic, relational, logical, and assignment operators. It provides examples of how each operator is used and the functions they perform. The key points are: - Operators are symbols used to manipulate values and variables and are divided into arithmetic, relational, and logical categories. - Arithmetic operators include addition, subtraction, multiplication, division, and modulo and are used to perform math operations on numeric data types. - Relational operators compare values and include less than, greater than, equal to, etc. - Logical operators combine conditional statements and include AND, OR, and NOT functions. - Assignment operators assign values to variables like

Uploaded by

Rohaiza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

PENGATURCARAAN

KOMPUTER
BBT 30802
BAB 5:
Expressions & Operator
OPERATOR
(Operator & Simbol)
Operator Symbol
Arithmetik +,-,*,/,%
Relational <,<=,>,>=,==,!=
Logik
&&,||,!
Simbol seperti +,-,*,/,<,> dikenali sebagai operator.
Data yang disimpan di dalam memori boleh diubahsuai
dengan menggunakan operator.
Operator dibahagikan kepada tiga (3) bentuk nyata
(distinct).
OPERATOR ARITMETIK

OPERASI
ARITMETIK
UNARI
BINARI
OPERATOR ARITMETIK
(Simbol Operator Unary)
Operator Fungsi
+ Positive operator
- Negative operator
++ Increment operator
-- Decrement operator
! Not operator
& Address operator
* Value of address operator
Operator Unary tertakluk kepada satu (1)
pembolehubah sahaja.
EKSPRESI
(EXPRESSIONS)
Kandungan
sebelum
Ekspresi Nilai bagi
ekspresi
Kandungan
selepas
10 a++ (a=a+1) 10 11
10 a-- 10 9
10 ++a (a=a+1) 11 11
10 --a 9 9
EKSPRESI
(EXPRESSIONS)
#include <stdio.h>
int main(void)
{
int a= 1, b=1, c=3, d=1;

c--;
printf(%d %d, 4-d++, c);
printf(%d %d %d %d \n, --a, b++, c--, d);

return 0;
}
EKSPRESI
(EXPRESSIONS)
Ekspresi Nilai x Nilai y
y+=x;
y=y+x;
5 12
y-= x++; 6 2
y*= ++x * 2; 6 84
Andaikan:
int x = 5;
int y = 7;
EKSPRESI
(EXPRESSIONS)
Ekspresi Nilai x Nilai y
y+=x; y+=x
y = y + x
= 7 + 5
= 12

5 12
Andaikan:

int x = 5;
int y = 7;
EKSPRESI
(EXPRESSIONS)
Ekspresi Nilai x Nilai y
y-= x++; y-=x++
y = y x++
= 7 5
= 2

6 2
Andaikan
int x = 5;
int y = 7;
EKSPRESI
(EXPRESSIONS)
Ekspresi Nilai x Nilai y
y*= ++x * 2; y*=++x * 2
y = y * (++x) * 2
= 7 * (6) * 2
= 42 * 2
= 84

6 84
Andaikan
int x = 5;
int y = 7;
OPERATOR ARITMETIK
Simbol Operator Binari

Operator FUngsi
+ Addition operator
- Subtraction operator
*
Multiplication operator
/
Division operator
%
Remainder operator (int only)
Operan yang dibaiki dengan operator arithmetik
mestilah jenis numerik.
Operator diletakkan antara dua (2) operan.
OPERATOR ARITMETIK
Simbol Operator Binari
(Binary Arithmetic Operators)
Nilai a Nilai b Pembahagi Integer Pembahagi Nyata
10 3 10/3 = 3 10.0/3.0=3.333333
5 10 5/10 = 0 5.0/10.0=0.500000
6 6 6/6 = 1 6.0/6.0=1.000000
Pengiraan Pembahagian (Division calculations)
OPERATOR ARITMETIK
(Binary Arithmetic Operators)
Operasi Keputusan Modulos Peringatan
5 % 3 1 2
7 % 2 3 1
12 % 2 6 0
Pengiraan Modulo
OPERATOR ARITMETIK
Bahagian Keutamaan (Precedence levels)
Gabungan satu atau lebih operasi arithmetik.
Bahagian keutamaan akan memansuhkan turutan
operasi arithmetik dalam penyelesaian ekspresi.
Precedence level Operation
High
()
++,--
*,/,%
+,-
+=,-=,*=,/=,%=
>,<,<=,>=
Low
==,!=
PERNYATAAN TUGASAN
(ASSIGNMENT STATEMENT)

Tugasan bagi data kepada pembolehubah boleh ditulis seperti
berikut:

Variable = expression;

Cth:-
Berat_Kereta = 85;
Diameter_Tayar = 18;
Horse_Power = Diameter_Tayar * ( 12 + Berat_Kereta);

Bagi kenyataan di atas, bahgian kiri dekenali sebagai
pembolehubah. Di sebelah kanan pula, merupakan kombinasi
kepada pembolehubah dan pemalar (constant).
PERNYATAAN TUGASAN
(Compound Assignment Statement)
Operasi Contoh Maksud
+= val += 5; val = val + 5
-= Val -= 5; val = val 5
*= val *= 5; val = val * 5
/= val /= 5; val = val / 5
%= val %= 5; val = val % 5
Pengubahsuaian nilai pembolehubah di mana nilai
pembolehubah asal ditambahkan, ditolak, didarab kepada
nilai yang lain dan dipindahkan kembali kepada
pembolehubah yang asal (original).
e.g:
price = price discount;

RELATIONAL OPERATOR

Operator Maksud
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
!=
Not equal to
Digunakan sebagai perbandingan.
OPERATOR LOGIK

Operator Fungsi Maksud
&&
And It is true if and only if both the
expressions are true
||
Or It is true if one or both the expressions
are true
!
Not It is true if the expression is false and
vice versa
Berguna dalam membuat perbandingan kompleks dalam
penyelesaian keputusan.

You might also like