Professional Documents
Culture Documents
Variables(Pembolehubah)
Variables are identifiers and the values its
hold can be change during the program is
run.
Reserved words (Kata kunci/simpanan)
Reserved words are define characters and
has a special purpose. For examples if, int,
const, else and ect..
Data Types and Declarations
An example of declaration
Syntax to declare the variable.
Jenis_Data nama_pembolehubah;
Eg :
int nom1;
char gred;
float tinggi;
Data Types and Declarations
Logical operators
Operators
There are seven arithmetic operators
in C++ such as
subtraction,
addition,
multiplication,
division,
modulus division,
decrement
increment.
Operators
a = 10, b = 2
Operator Tindakan Ungkapan Nilai
- Tolak ab 8
+ Tambah a+b 12
* Darab a*b 20
/ Bahagi a/b 5
% Pembahagian a%b 0
modulus
-- Pengurangan (prefix) --a 9
++ Penambahan(postfix) b++ 3
Operators
Precedence (Keutamaan)
1. Tertinggi ++, --
2. Pertengahan *, /, %
(dari kiri ke kanan)
3. Terendah +, -
(dari kiri ke kanan)
Operators
The arithmetic expression will be
evaluated based on the operator
precedence. The highest operator
precedence will be evaluated first.
Example:
2+7*5
= 2 + 35
= 37
If the level of operator precedence at
the same level, the expression will be
evaluated from left to right.
Example:
2*3/2
=6/2
=3
Parentheses can be used to force the order of
evaluation. So, the evaluation is not based on
the operator precedence.
Example:
(9 (3 + 2)) * 3
= (9 5) * 3
= 4*3
= 12
Operators
Lets consider the following example:
int a=5, b=2, c=3, d=4;
( a + c) / (d / b) * a;
Increment And Decrement
Operators
Dalam C++, ia mempunyai dua operator
istimewa iaitu penambahan dan
pengurangan. Selalunya pernyataan seperti
y=y+1
akan diringkaskan menjadi
y++
dan ia mempunyai makna yang sama. Di
samping itu kita juga boleh menulis dalam
bentuk
++ y
Increment And Decrement
Operators
Tetapi perlu diingat bahawa penambahan
hanya melibatkan satu nombor sahaja dan
tidak melibatkan nombor-nombor lain.
Manakala operator pengurangan seperti
pernyataan
y=y1
akan diringkaskan menjadi
y--
dan ia mempunyai makna yang sama. Di
samping itu kita juga boleh menulis dalam
bentuk
-- y.
Increment And Decrement
Operators
#include<iostream.h>
void main()
{
int c;
c = 5;
cout<< c << endl; //print 5
cout<< c++ << endl; //print 5 then postincrement
cout<< c << endl << endl; //print 6
c = 5;
cout<< c << endl; //print 5
cout<< ++c << endl; //preincrement then print 6
cout<< c << endl; //print 6
}
Increment And Decrement
Operators
Sebagai contoh pertimbangkan contoh
berikut:
int a,b, c;
Kita anggap nilai semasa bagi
pembolehubah a dan b adalah 10 dan
20,
c = 2 * -- a + b;
Cast function
The function cast may be used to force
an expression to be of a specific data
type.
Syntax:
(type) expression;
Example:
int b;
double a;
a = 3.4 , b = 12;
Expression Output
a = (int)(a + b) 15
a = a + (double)b 15.4
a = (int) a % b 3
a = b % (int)a 0
a = (int)a + b 15
Operator Persamaan
Kompound
Berikut adalah operator persamaan kompound.
Tambah , +=
Tolak, -=
Darab, *=
Bahagi, /=
Baki, %=
Operator persamaan kompound membolehkan
kita meringkaskan sesuatu formula. Sebagai
contoh persamaan ditulis sebagai,
x=x+5
Jadi persamaan ini boleh diringkaskan menjadi
x += 5
Relational Operators
Terdapat 6 operator hubungan dalam C++ dan sebagai contoh di beri
nilai x = 5 dan y =2.