In diesem Tutorial lernen wir anhand von Beispielen relationale und logische Operatoren kennen.
In C ++ vergleichen relationale und logische Operatoren zwei oder mehr Operanden und geben entweder true
oder false
Werte zurück.
Wir verwenden diese Operatoren bei der Entscheidungsfindung.
C ++ - Vergleichsoperatoren
Ein Vergleichsoperator wird verwendet, um die Beziehung zwischen zwei Operanden zu überprüfen. Beispielsweise,
// checks if a is greater than b a> b;
Hier >
ist ein Vergleichsoperator. Es wird geprüft, ob a größer als b ist oder nicht.
Wenn die Beziehung wahr ist , gibt sie 1 zurück, während sie, wenn die Beziehung falsch ist , 0 zurückgibt .
In der folgenden Tabelle sind die in C ++ verwendeten Vergleichsoperatoren zusammengefasst.
Operator | Bedeutung | Beispiel |
---|---|---|
== | Entspricht | 3 == 5 gibt uns falsch |
!= | Nicht gleichzusetzen mit | 3 != 5 gibt uns wahr |
> | Größer als | 3> 5 gibt uns falsch |
< | Weniger als | 3 < 5 gibt uns wahr |
>= | Größer als oder gleich wie | 3>= 5 gib uns falsch |
<= | Weniger als oder gleich | 3 <= 5 gibt uns wahr |
== Operator
Der ==
Operator " gleich" wird zurückgegeben
true
- wenn beide Operanden gleich oder gleich sindfalse
- wenn die Operanden ungleich sind
Beispielsweise,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Hinweis: Der Vergleichsoperator ==
ist nicht mit dem Zuweisungsoperator identisch =
. Der Zuweisungsoperator =
weist einer Variablen, Konstante, einem Array oder einem Vektor einen Wert zu. Es werden nicht zwei Operanden verglichen.
! = Operator
Der ungleich !=
Operator kehrt zurück
true
- wenn beide Operanden ungleich sindfalse
- wenn beide Operanden gleich sind.
Beispielsweise,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Betreiber
Der >
Operator größer als kehrt zurück
true
- wenn der linke Operand größer als der rechte istfalse
- wenn der linke Operand kleiner als der rechte ist
Beispielsweise,
int x = 10; int y = 15; x> y // false y> x // true
<Operator
Der Operator kleiner als <
kehrt zurück
true
- wenn der linke Operand kleiner als der rechte istfalse
- wenn der linke Operand größer als der rechte ist
Beispielsweise,
int x = 10; int y = 15; x < y // true y < x // false
> = Operator
Der >=
Operator größer oder gleich return
true
- wenn der linke Operand größer oder gleich dem rechten istfalse
- wenn der linke Operand kleiner als der rechte ist
Beispielsweise,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operator
Der Operator kleiner oder gleich gibt <=
zurück
true
- wenn der linke Operand kleiner oder gleich dem rechten istfalse
- wenn der linke Operand größer als der rechte ist
Beispielsweise,
int x = 10; int y = 15; x> y // false y> x // true
In unserem Tutorial hier erfahren Sie, wie relationale Operatoren mit Zeichenfolgen verwendet werden können.
Logische C ++ - Operatoren
Wir verwenden logische Operatoren, um zu überprüfen, ob ein Ausdruck wahr oder falsch ist . Wenn der Ausdruck wahr ist , wird 1 zurückgegeben, während wenn der Ausdruck falsch ist , 0 zurückgegeben wird .
Operator | Beispiel | Bedeutung |
---|---|---|
&& | Ausdruck1 && Ausdruck 2 | Logisches UND. true nur, wenn alle Operanden true sind. |
|| | Ausdruck1 || Ausdruck 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.
Wahrheitstabelle der! Operator
Sei a ein Operand. Dann,
Beispiel 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; )
Ausgabe
1 0
In diesem Programm deklarieren und initialisieren wir eine int
Variable a mit dem Wert 5
. Wir drucken dann einen logischen Ausdruck
!(a == 0)
Hier wird a == 0
ausgewertet, false
wie der Wert von a ist 5
. Wir verwenden jedoch den Operator NOT !
an a == 0
. Da a == 0
ausgewertet wird false
, !
invertiert der Bediener die Ergebnisse von a == 0
und das Endergebnis ist true
.
In ähnlicher Weise !(a == 5)
kehrt der Ausdruck letztendlich zurück, false
weil a == 5
is true
.