In deze tutorial leren we aan de hand van voorbeelden over relationele en logische operatoren.
In C ++ vergelijken relationele en logische operatoren twee of meer operanden en retourneren een trueof meer falsewaarden.
We gebruiken deze operators bij het nemen van beslissingen.
C ++ relationele operatoren
Een relationele operator wordt gebruikt om de relatie tussen twee operanden te controleren. Bijvoorbeeld,
// checks if a is greater than b a> b;
Hier >is een relationele operator. Het controleert of a groter is dan b of niet.
Als de relatie waar is , retourneert deze 1, terwijl als de relatie onwaar is , deze 0 retourneert .
De volgende tabel geeft een overzicht van de relationele operatoren die in C ++ worden gebruikt.
| Operator | Betekenis | Voorbeeld |
|---|---|---|
== | Is gelijk aan | 3 == 5geeft ons vals |
!= | Niet gelijk aan | 3 != 5geeft ons waar |
> | Groter dan | 3> 5geeft ons vals |
< | Minder dan | 3 < 5geeft ons waar |
>= | Groter dan of gelijk aan | 3>= 5geef ons vals |
<= | Minder dan of gelijk aan | 3 <= 5geeft ons waar |
== Operator
De ==operator is gelijk aan retourneert
true- als beide operanden gelijk of hetzelfde zijnfalse- als de operanden ongelijk zijn
Bijvoorbeeld,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Opmerking: de relationele operator ==is niet dezelfde als de toewijzingsoperator =. De toewijzingsoperator =wijst een waarde toe aan een variabele, constante, matrix of vector. Het vergelijkt twee operanden niet.
! = Operator
De !=operator niet gelijk aan retourneert
true- als beide operanden ongelijk zijnfalse- als beide operanden gelijk zijn.
Bijvoorbeeld,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operator
De >operator groter dan keert terug
true- als de linker operand groter is dan de rechterfalse- als de linker operand kleiner is dan de rechter
Bijvoorbeeld,
int x = 10; int y = 15; x> y // false y> x // true
<Operator
De operator minder dan <keert terug
true- als de linker operand kleiner is dan de rechterfalse- als de linker operand groter is dan rechts
Bijvoorbeeld,
int x = 10; int y = 15; x < y // true y < x // false
> = Operator
De >=operator groter dan of gelijk aan retourneert
true- als de linker operand groter is dan of gelijk is aan rechtsfalse- als de linker operand kleiner is dan de rechter
Bijvoorbeeld,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operator
De operator kleiner dan of gelijk aan <=retourneert
true- als de linker operand kleiner dan of gelijk is aan rechtsfalse- als de linker operand groter is dan rechts
Bijvoorbeeld,
int x = 10; int y = 15; x> y // false y> x // true
Raadpleeg onze tutorial hier om te leren hoe relationele operatoren kunnen worden gebruikt met strings.
C ++ logische operators
We gebruiken logische operatoren om te controleren of een uitdrukking waar of onwaar is . Als de uitdrukking waar is , retourneert deze 1, terwijl als de uitdrukking onwaar is , deze 0 retourneert .
| Operator | Voorbeeld | Betekenis |
|---|---|---|
&& | expression1 && expression 2 | Logisch EN. alleen waar als alle operanden waar zijn. |
|| | expressie1 || uitdrukking 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.
Waarheidstabel van de! Operator
Laat a een operand zijn. Vervolgens,
Voorbeeld 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; )
Uitvoer
1 0
In dit programma declareren en initialiseren we een intvariabele a met de waarde 5. We drukken dan een logische uitdrukking af
!(a == 0)
Hier a == 0evalueert falseals de waarde van a is 5. We gebruiken echter de NOT-operator !op a == 0. Aangezien a == 0evalueert tot false, !keert de operator de resultaten van a == 0en het uiteindelijke resultaat is true.
Evenzo !(a == 5)retourneert de uitdrukking uiteindelijk falseomdat a == 5is true.








