Melakukan Operasi Logika
• Hampir semua statemen C++ adalah ekspresi. • Operator C++ selain +, -, /, * yakni operator logika. • Pada dasarnya orang2 menghitung menggunakan operasi AND dan OR
Mengapa Menggunakan Operasi Logika • Program C++ harus membuat Keputusan • Membuat keputusan benar atau salah, butuh operasi logika
Tabel
• Contoh n == 0; • Apa bedanya dengan n = 0 ? • Contoh Operator lebih besar (>) atau lebih kecil (<) : int n1 = 1; int n2 = 2; n1 < n2;
• Contoh kombinasi operator && (AND), II (OR) dengan operator logika yang lain : // true if n2 is greater than n1 but n2 smaller than n3 // (this is the most common way determining that n2 is in // the range of n1 to n3, exclusive) (n1 < n2) && (n2 < n3);
Menyimpan Nilai Logika • Hasil dari operasi logika dapat disimpan ke variabel yang bertipe bool : int n1 = 1; int n2 = 2; bool b; b = (n1 == n2);
• Apa bedanya antara operasi (=) dengan (==)
Program Mendemonstrasikan Variable bool // BoolTest - compare variables input from the // keyboard and store the results off // into a logical variable #include
#include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // set output format for bool variables // to true and false instead // of 1 and 0 cout.setf(cout.boolalpha);
// initialize two arguments int nArg1; cout << “Input value 1: “; cin >> nArg1; int nArg2; cout << “Input value 2: “; cin >> nArg2; bool b; b = nArg1 == nArg2;
cout << “The statement, “ << nArg1 << “ equals “ << nArg2 << “ is “ << b << endl; // wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; }
• Tampilan program setelah di eksekusi : Input value 1: 5 Input value 2: 5 The statement, 5 equals 5 is true Press any key to continue . . .
Warning : Penggunaan Operasi Logika Pada Floating Point float f1 = 10.0; float f2 = f1 / 3; f1 == (f2 * 3.0); // are these two equal?
• Perbandingan ini tidak terlalu dibutuhkan.
• Perhatikan problem berikut : float f1 = 10.0; float f2 = 100 % 30; f1 == f2; // are these two equal?
• Perhatikan Command Berikut : float float float (f1 -
f1 = 10.0; f2 = f1 / 3; f3 = f2 * 3.0; f3) < 0.0001 && (f3 - f1) < 0.0001;
Sistem Bilangan • Sistem Bilangan Desimal : 123 = 1 * 100 + 2 * 10 + 3 * 1
atau 123 = 1 * 102 + 2 * 101 + 3 * 100
• Sistem Bilangan Octal : 12310 = 1 * 82 + 7 * 81 + 3 * 80 = 1738
• Sistem Bilangan Biner : 12310 = 0*128 + 1*64 + 1*32 + 1*16 + 1*8 + 0*4 +1*2 + 1*1 = 011110112
• Sistem Bilangan Hexadecimal : 123 = 7 * 161 + B (i.e. 11) * 160 = 7B16
Melakukan Operasi bitwise C++ • Semua bilangan C++ diekspresikan ke dalam bentuk bilangan biner :
Operator • Operator NOT : NOT 1 (true) is 0 (false) NOT 0 (false) is 1 (true)
• Operator AND : 1 (true) AND 1 (true) is 1 (true) 1 (true) AND 0 (false) is 0 (false)
• Operator OR : 1 (true) OR 0 (false) is 1 (true) 0 (false) OR 0 (false) is 0 (false)
Bitwise Operator
Penggunaan Operator Bitwise • Operator NOT : ~01102 (0x6) 10012 (0x9)
• Dapat kita simpulkan ~(0x6) = (0x9) • Operator & : 01102 & 00112 00102
0x6 & 0x3 0x2
01102 & 00112 00102
• Dapat kita simpulkan : 0x6 & 0x3 = 0x2 • Tes : Berapakah nilai 0x6 & 0x3 ?
Simple Test • Program berikut mengilustrasikan penggunaan operator bitwise :
// BitTest - initialize two variables and output the // results of applying the ~,& , | and ^ // operations #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // set output format to hexadecimal cout.setf(cout.hex);
// initialize two arguments int nArg1; nArg1 = 0x1234; int nArg2; nArg2 = 0x00ff; // now perform each operation in turn // first the unary NOT operator cout << “Arg1 = 0x” << nArg1 << “\n”; cout << “Arg2 = 0x” << nArg2 << “\n”; cout << “~nArg1 = 0x” << ~nArg1 << “\n”; cout << “~nArg2 = 0x” << ~nArg2 << “\n”;
// now the binary operators cout << “nArg1 & nArg2 = 0x” << (nArg1 & nArg2) << “\n”; cout << “nArg1 | nArg2 = 0x” << (nArg1 | nArg2) << “\n”; cout << “nArg1 ^ nArg2 = 0x” << (nArg1 ^ nArg2) << “\n”; // wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; }
• Ekspresi pertama dari program kita, cout.setf(ios::hex), Mengeset format output dari default desimal ke hexadesimal.
• Proses Program : Arg1 = 0x1234 Arg2 = 0xff ~nArg1 = 0xffffedcb ~nArg2 = 0xffffff00 nArg1 & nArg2 = 0x34 nArg1 | nArg2 = 0x12ff nArg1 ^ nArg2 = 0x12cb Press any key to continue . . .
Mengontrol Aliran Program
Mengontrol Aliran Program • Bentuk sederhana dari kontrol aliran adalah branch statement (statemen cabang) • Dalam C++, branch statement diimplementasikan statement if : if (m > n) { // Path 1 // ...instructions to be executed if // m is greater than n } else { // Path 2 // ...instructions to be executed if not }
• Program mendemonstrasikan statement if : // BranchDemo - input two numbers. Go down one path of the // program if the first argument is greater than // the first or the other path if not #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // input the first argument... int arg1; cout << “Enter arg1: “; cin >> arg1;
// ...and the second int arg2; cout << “Enter arg2: “; cin >> arg2; // now decide what to do: if (arg1 > arg2) { cout << “Argument 1 is greater than argument 2” << endl; } else { cout << “Argument 1 is not greater than argument 2” << endl; }
// wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; }
• Program ini membaca dua bilangan integer dari keyboard dan membandingkannya.
• Hasil Eksekusi : Enter arg1: 5 Enter arg2: 6 Argument 1 is not greater than argument 2 Press any key to continue . . .
Looping Saat Kondisi bernilai TRUE • Bentuk paling sederhana dari statement looping adalah loop while while(condition) { // ... repeatedly executed as long as condition is true }
• Contoh Program : // WhileDemo - input a loop count. Loop while // outputting astring arg number of times. #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // input the loop count int loopCount; cout << “Enter loopCount: “; cin >> loopCount;
// now loop that many times while (loopCount > 0) { loopCount = loopCount - 1; cout << “Only “ << loopCount << “ loops to go\n”; } // wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; }
• Hasil eksekusi : Enter loopCount: 5 Only 4 loops to go Only 3 loops to go Only 2 loops to go Only 1 loops to go Only 0 loops to go Press any key to continue . . .
• Jika User memasukkan nilai negatif, maka program tidak melewati loop. • Versi lain dari While yang jarang di gunakan yakni do . . . While do { // ...the inside of the loop } while (condition);
• Apa perbedaannya dengan while ?
Penggunaan Fitur autoincrement / autodecrement // now loop that many times while (loopCount > 0) { loopCount = loopCount - 1; cout << “Only “ << loopCount << “ loops to go\n”; }
• Versi lain: while (loopCount > 0) { loopCount--; cout << “Only “ << loopCount << “ loops to go\n”; }
• Dengan Autodecrement : while (loopCount-- > 0) { cout << “Only “ << loopCount << “ loops to go\n”; }
• Versi program yang menggunakan Autodecrement akan tereksekusi lebih cepat