W tym samouczku dowiemy się o operatorach relacyjnych i logicznych na przykładach.
W języku C ++ operatory relacyjne i logiczne porównują dwa lub więcej operandów i zwracają albo true
albo false
wartości.
Korzystamy z tych operatorów przy podejmowaniu decyzji.
Operatory relacyjne C ++
Operator relacyjny służy do sprawdzania relacji między dwoma operandami. Na przykład,
// checks if a is greater than b a> b;
Tutaj >
jest operatorem relacji. Sprawdza, czy a jest większe od b, czy nie.
Jeśli relacja jest prawdziwa , zwraca 1, podczas gdy jeśli relacja jest fałszywa , zwraca 0 .
W poniższej tabeli podsumowano operatory relacyjne używane w języku C ++.
Operator | Znaczenie | Przykład |
---|---|---|
== | Jest równe | 3 == 5 daje nam fałsz |
!= | Nie równe | 3 != 5 daje nam prawdę |
> | Lepszy niż | 3> 5 daje nam fałsz |
< | Mniej niż | 3 < 5 daje nam prawdę |
>= | Większy lub równy | 3>= 5 daj nam fałsz |
<= | Mniejszy lub równy | 3 <= 5 daje nam prawdę |
== Operator
Operator równa się ==
zwraca
true
- jeśli oba operandy są równe lub takie samefalse
- jeśli operandy są nierówne
Na przykład,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Uwaga: Operator relacyjny ==
to nie to samo, co operator przypisania =
. Operator przypisania =
przypisuje wartość do zmiennej, stałej, tablicy lub wektora. Nie porównuje dwóch operandów.
! = Operator
Operator nie równa się !=
zwraca
true
- jeśli oba operandy są nierównefalse
- jeśli oba operandy są równe.
Na przykład,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operator
Większe niż >
zwraca operator
true
- jeśli lewy operand jest większy niż prawyfalse
- jeśli lewy operand jest mniejszy niż prawy
Na przykład,
int x = 10; int y = 15; x> y // false y> x // true
<Operator
Operator mniej niż <
zwraca
true
- jeśli lewy operand jest mniejszy niż prawyfalse
- jeśli lewy operand jest większy niż prawy
Na przykład,
int x = 10; int y = 15; x < y // true y < x // false
> = Operator
Operator większy lub równy >=
zwraca
true
- jeśli lewy operand jest większy lub równy prawemufalse
- jeśli lewy operand jest mniejszy niż prawy
Na przykład,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operator
Operator mniejszy lub równy <=
zwraca
true
- jeśli lewy operand jest mniejszy lub równy prawemufalse
- jeśli lewy operand jest większy niż prawy
Na przykład,
int x = 10; int y = 15; x> y // false y> x // true
Aby dowiedzieć się, jak operatory relacyjne mogą być używane z łańcuchami, zapoznaj się z naszym samouczkiem tutaj.
Operatory logiczne C ++
Używamy operatorów logicznych, aby sprawdzić, czy wyrażenie jest prawdziwe, czy fałszywe . Jeśli wyrażenie jest prawdziwe , zwraca 1, podczas gdy jeśli wyrażenie jest fałszywe , zwraca 0 .
Operator | Przykład | Znaczenie |
---|---|---|
&& | wyrażenie1 && wyrażenie 2 | Logiczne AND. prawdziwe tylko wtedy, gdy wszystkie operandy są prawdziwe. |
|| | wyrażenie1 || wyrażenie 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.
Tabela prawdy! Operator
Niech a będzie operandem. Następnie,
Przykład 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; )
Wynik
1 0
W tym programie deklarujemy i inicjalizujemy int
zmienną a wartością 5
. Następnie drukujemy wyrażenie logiczne
!(a == 0)
Tutaj a == 0
szacuje się false
jako wartość a jest 5
. Jednak używamy operatora NOT !
na a == 0
. Ponieważ a == 0
wartościuje do false
, !
operator odwraca wyniki, a == 0
a wynik końcowy to true
.
Podobnie wyrażenie !(a == 5)
ostatecznie zwraca, false
ponieważ a == 5
jest true
.