Dalam tutorial ini, kita akan belajar tentang operator relasional dan logika dengan bantuan contoh.
Dalam C ++, operator relasional dan logika membandingkan dua atau lebih operan dan mengembalikan salah satu true
atau false
nilai.
Kami menggunakan operator ini dalam pengambilan keputusan.
Operator Relasional C ++
Operator relasional digunakan untuk memeriksa hubungan antara dua operan. Sebagai contoh,
// checks if a is greater than b a> b;
Di sini, >
adalah operator relasional. Ia memeriksa apakah a lebih besar dari b atau tidak.
Jika relasinya benar , ia mengembalikan 1 sedangkan jika relasinya salah , ia mengembalikan 0 .
Tabel berikut merangkum operator relasional yang digunakan dalam C ++.
Operator | Berarti | Contoh |
---|---|---|
== | Adalah sama dengan | 3 == 5 memberi kami palsu |
!= | Tidak sebanding dengan | 3 != 5 memberi kami kebenaran |
> | Lebih besar dari | 3> 5 memberi kami palsu |
< | Kurang dari | 3 < 5 memberi kami kebenaran |
>= | Lebih dari atau sama dengan | 3>= 5 beri kami palsu |
<= | Kurang Dari atau Sama Dengan | 3 <= 5 memberi kami kebenaran |
== Operator
Sama dengan ==
pengembalian operator
true
- jika kedua operannya sama atau samafalse
- jika operannya tidak sama
Sebagai contoh,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Catatan: Operator relasional ==
tidak sama dengan operator penugasan =
. Operator penugasan =
memberikan nilai ke variabel, konstanta, larik, atau vektor. Itu tidak membandingkan dua operan.
! = Operator
Tidak sama dengan !=
pengembalian operator
true
- jika kedua operan tidak samafalse
- jika kedua operan sama.
Sebagai contoh,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operator
Lebih besar dari >
pengembalian operator
true
- jika operan kiri lebih besar dari kananfalse
- jika operan kiri kurang dari kanan
Sebagai contoh,
int x = 10; int y = 15; x> y // false y> x // true
<Operator
Kurang dari <
pengembalian operator
true
- jika operan kiri kurang dari kananfalse
- jika operan kiri lebih besar dari kanan
Sebagai contoh,
int x = 10; int y = 15; x < y // true y < x // false
> = Operator
Lebih besar dari atau sama dengan >=
pengembalian operator
true
- jika operan kiri lebih besar dari atau sama dengan kananfalse
- jika operan kiri kurang dari kanan
Sebagai contoh,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operator
Kurang dari atau sama dengan <=
pengembalian operator
true
- jika operan kiri kurang dari atau sama dengan kananfalse
- jika operan kiri lebih besar dari kanan
Sebagai contoh,
int x = 10; int y = 15; x> y // false y> x // true
Untuk mempelajari bagaimana operator relasional dapat digunakan dengan string, lihat tutorial kami di sini.
Operator Logis C ++
Kami menggunakan operator logika untuk memeriksa apakah ekspresi benar atau salah . Jika ekspresi benar , ia mengembalikan 1 sedangkan jika ekspresi salah , ia mengembalikan 0 .
Operator | Contoh | Berarti |
---|---|---|
&& | ekspresi1 && ekspresi 2 | DAN logis. benar hanya jika semua operannya benar. |
|| | ekspresi1 || ekspresi 2 | Logical OR. true if at least one of the operands is true. |
! | !expression | Logical NOT. true only if the operand is false. |
C++ Logical AND Operator
The logical AND operator &&
returns
true
- if and only if all the operands aretrue
.false
- if one or more operands arefalse
.
Truth Table of && Operator
Let a and b be two operands. 0 represents false while 1 represents true. Then,
a | b | a && b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
As we can see from the truth table above, the &&
operator returns true only if both a
and b
are true.
Note: The Logical AND operator && should not be confused with the Bitwise AND operator &.
Example 1: C++ OR Operator
// C++ program demonstrating && operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = false cout << ((a == 0) && (a < b)) << endl; // true && false = false cout < b)) << endl; // true && true = true cout << ((a == 5) && (a < b)) << endl; return 0; )
Output
0 0 0 1
In this program, we declare and initialize two int
variables a and b with the values 5
and 9
respectively. We then print a logical expression
((a == 0) && (a> b))
Here, a == 0
evaluates to false
as the value of a is 5
. a> b
is also false
since the value of a is less than that of b. We then use the AND operator &&
to combine these two expressions.
From the truth table of &&
operator, we know that false && false
(i.e. 0 && 0
) results in an evaluation of false
(0
). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of the &&
operator.
C++ Logical OR Operator
The logical OR operator ||
returns
true
- if one or more of the operands aretrue
.false
- if and only if all the operands arefalse
.
Truth Table of || Operator
Let a and b be two operands. Then,
a | b | a || b |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
As we can see from the truth table above, the ||
operator returns false only if both a
and b
are false.
Example 2: C++ OR Operator
// C++ program demonstrating || operator truth table #include using namespace std; int main() ( int a = 5; int b = 9; // false && false = false cout < b)) << endl; // false && true = true cout << ((a == 0) || (a < b)) << endl; // true && false = true cout < b)) << endl; // true && true = true cout << ((a == 5) || (a < b)) << endl; return 0; )
Output
0 1 1 1
In this program, we declare and initialize two int
variables a and b with the values 5
and 9
respectively. We then print a logical expression
((a == 0) || (a> b))
Here, a == 0
evaluates to false
as the value of a is 5
. a> b
is also false
since the value of a is less than that of b. We then use the OR operator ||
to combine these two expressions.
From the truth table of ||
operator, we know that false || false
(i.e. 0 || 0
) results in an evaluation of false
(0
). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of ||
operator.
C++ Logical NOT Operator !
The logical NOT operator !
is a unary operator i.e. it takes only one operand.
It returns true when the operand is false, and false when the operand is true.
Tabel Kebenaran! Operator
Biarkan seorang menjadi operan. Kemudian,
Contoh 3: C ++! Operator
// C++ program demonstrating ! operator truth table #include using namespace std; int main() ( int a = 5; // !false = true cout << !(a == 0) << endl; // !true = false cout << !(a == 5) << endl; return 0; )
Keluaran
1 0
Dalam program ini, kami mendeklarasikan dan menginisialisasi int
variabel a dengan nilai 5
. Kami kemudian mencetak ekspresi logis
!(a == 0)
Di sini, a == 0
dievaluasi false
sebagai nilai a is 5
. Namun, kami menggunakan operator NOT !
di a == 0
. Sejak a == 0
mengevaluasi ke false
, !
operator membalikkan hasil a == 0
dan hasil akhirnya adalah true
.
Demikian pula, ekspresi !(a == 5)
akhirnya kembali false
karena a == 5
adalah true
.