alpro - operator - ifrozi.files.wordpress.com ·...
TRANSCRIPT
Imam Fahrur Rozi
Algoritma dan Pemrograman
OPERATOR
OPERATOR PENUGASAN (ASSIGNMENT)
² Operator = ² Digunakan untuk memberikan nilai ke dalam suatu variabel ² Contoh:
² a = 5; ² a = b = c = 5; ² a = b; ² a = 2 + (b = 5); ekivalen dengan b = 5; a = 2 + b;
OPERATOR PENUGASAN (ASSIGNMENT)
// assignment operator #include <iostream> using namespace std; int main () { int a, b; // a:?, b:? a = 10; // a:10, b:? b = 4; // a:10, b:4 a = b; // a:4, b:4 b = 7; // a:4, b:7 cout << "a:"; cout << a <<endl; cout << " b:"; cout << b; return 0; }
Output: a:4 b:7
OPERATOR ARITMATIKA
² Operator untuk perhitungan aritmaHka
² Contoh: ² a = b + c * d; ² x = a % 2; ² dsb
+ penambahan -‐ pengurangan * perkalian / pembagian % sisa pembagian
OPERATOR PENUGASAN GABUNGAN
² Terdiri dari: +=, -‐=, *=, /=, %=, >>=, <<=, &=, ^=, |= ² Digunakan keHka kita ingin memodifikasi suatu nilai yang tersimpan dalam suatu variabel dengan cara melakukan suatu operasi terhadap nilai yang sedang tersimpan dalam variabel itu sendiri .
² Contoh:
² jum = jum + 10; sama dengan jum += 10 ² i = i + 1; sama dengan i += 1;
expression is equivalent to value += increase; value = value + increase; a -‐= 5; a = a -‐ 5; a /= b; a = a / b; price *= units + 1; price = price * (units + 1);
OPERATOR PENUGASAN GABUNGAN
// compound assignment operators #include <iostream> using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; }
Output: 5
OPERATOR INCREMENT DAN DECREMENT
² Terdiri dari: ++ dan -‐-‐ ² Digunakan untuk menambah 1 atau mengurangi1 terhadap nilai yang sedang tersimpan dalam suatu variabel
² a++;, sama dengan a += 1;, sama dengan a = a+1; ² a-‐-‐;, sama dengan a -‐= 1;, sama dengan a = a-‐1; ² Ada dua macam penulisan, yaitu ++ atau -‐-‐ ditulis di belakang variabel (a++ atau a-‐-‐), atau ditulis di depan variabel (++a atau -‐-‐a)
² Meskipun secara esensi kedua penulisan tsb akan menghasilkan nilai yang sama, tapi ada kalanya akan berdampak berbeda
Example 1 Example 2 B=3; B=3; A=++B; A=B++; // A contains 4, B contains 4 // A contains 3, B contains 4
OPERATOR PENUGASAN GABUNGAN
// increment and decrement operators #include <iostream> using namespace std; int main () { int a, b=3; a = 5; cout << a << endl; cout << a++ << endl; cout << a << endl; cout << “----” << endl; cout << b << endl; cout << ++b << endl; cout << b << endl; return 0; }
Output: 5 5 6 -‐-‐-‐-‐ 3 4 4
OPERATOR RELASIONAL
² Digunakan untuk membandingkan antara beberapa nilai ² Hasil dari operator ini adalah benar/true atau salah/false
== Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
OPERATOR RELASIONAL
(7 == 5) // evaluates to false. (5 > 4) // evaluates to true. (3 != 2) // evaluates to true. (6 >= 6) // evaluates to true. (5 < 5) // evaluates to false.
(a == 5) // evaluates to false since a is not equal to 5. (a*b >= c) // evaluates to true since (2*3 >= 6) is true. (b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false. ((b=2) == a) // evaluates to true.
OPERATOR LOGIKA
² Terdiri dari !, && dan || ² ! Digunakan untuk melakukan operasi komplemen boolean ² && dan || digunakan untuk mengevaluasi 2 buah ekspresi untuk menghasilkan satu hasil relasi.
² Dalam logika && adalah AND, serta || adalah OR
a b a && b true true true true false false false true false false false false
a b a || b true true true true false true false true true false false false
OPERATOR LOGIKA
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true.
( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).
OPERATOR LOGIKA DISEBUT JUGA SHORT-‐CIRCUIT
operator short-‐circuit && if the leh-‐hand side expression is false, the combined result is false (right-‐hand side expression not evaluated). || if the leh-‐hand side expression is true, the combined result is true (right-‐hand side expression not evaluated). if ((i<10)&&(++i<n)) { /*...*/ } Jika nilai i >= 10 atau ekspresi i<10 di atas menjadi false, makaekspresi ++i<n Hdak akan dijalankan atau Hdak dieksekusi
OPERATOR KONDISIONAL
² Operator kondisional akan mengevaluasi suatu ekspresi, yang jika ekspresi tersebut bernilai true akan menghasilkan sesuatu tertentu, dan jika salah akan menghasilkan sesuatu yang berbeda
² Formatnya ² condition ? Result 1 : result 2;
7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5. 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2. 5>3 ? a : b // returns the value of a, since 5 is greater than 3. a>b ? a : b // returns whichever is greater, a or b.
OPERATOR KONDISIONAL
// conditional operator #include <iostream.h> int main () { int a,b,c; a=2; b=7; c = (a>b) ? a : b; cout << c; return 0; }
Output: 7
OPERATOR BITWISE
² Operator bitwise akan mengoperasikan nilai biner dari suatu bilangan yang tersimpan dalam suatu variabel operator asm equivalent descripUon & AND Bitwise AND | OR Bitwise Inclusive OR ^ XOR Bitwise Exclusive OR ~ NOT Unary complement (bit
inversion) << SHL Shih Leh >> SHR Shih Right
OPERATOR BITWISE
int c = 2 & 1; cout << c Output: 0 Analisis: 2 = 10 1 = 01 Hasil AND = 00 = 0
OPERATOR CASTING
² Digunakan untuk mengubah Hpedata dari suatu nilai variabel ² Format penulisannya adalah dengan meletakkan tipe data
tujuan dalam tanda kurung, di depan nilai variabel yang akan diubah tipedatanya
int i; float f = 3.14; i = (int) f;
OPERATOR SIZEOF
² Digunakan untuk mengambil ukuran (dalam byte) dari suatu Hpe data
² Dari contoh tersebut a akan bernilai 1, karena ukuran tipedata char adalah 1 byte
a = sizeof (char)
Selamat Belajar
Algoritma dan Pemrograman
Serious, Focus, stay cool & calm JJJJJJJJJ