Jika sebelumnya kita pernah membahas mengenai tipe data bool
yang terdiri dari true dan false kali ini kita akan membahas mengenai operator
logis mungkin disini saya tidak perlu menjelaskan apa berbelit belit apa
itu operator logis karena saya yakin anda semua pasti mengetahui ini. Operator
logis itu sendiri terdiri dari :
1. And > P &&
Q
2. Or
> P || Q
3. Not > !P
Contoh program yang saya akan buat kali ini pun masih
mengandalkan tipe data bool kenapa karena tipe bool hanya mengijinkan angka 1
dan 0 atau true dan false. Silakan copy paste atau edit terserah anda source
code program berikut yang saya tulis di devc++.
1. Not
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
bool bP = true;
cout << “P = ” << bP << “ Not
P = ” << !bP << endl;
bP = false;
cout << “P = ” << bP << “ Not
P = ” << !bP << endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}
Hasilnya adalah sebagai berikut :
2. Or
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
bool bP = true;
bool bQ = true;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP || bQ)
<< endl;
bQ = false;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP || bQ)
<< endl;
bP = false;
bQ = true;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP || bQ)
<< endl;
bQ = false;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP || bQ)
<< endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}
Hasilnya adalah sebagai berikut:
3. And
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
bool bP = true;
bool bQ = true;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP &&
bQ) << endl;
bQ = false;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP &&
bQ) << endl;
bP = false;
bQ = true;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP &&
bQ) << endl;
bQ = false;
cout << “P = ” << bP << “ Q =
” << bQ << “ P or Q = ” << (bP &&
bQ) << endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}
Hasilnya adalah sebagai berikut:



No comments:
Post a Comment