In questo tutorial impareremo a conoscere gli operatori relazionali e logici con l'aiuto di esempi.
In C ++, gli operatori relazionali e logici confrontano due o più operandi e restituiscono uno dei valori true
o false
.
Usiamo questi operatori nel processo decisionale.
Operatori relazionali C ++
Un operatore relazionale viene utilizzato per verificare la relazione tra due operandi. Per esempio,
// checks if a is greater than b a> b;
Ecco >
un operatore relazionale. Controlla se a è maggiore di b oppure no.
Se la relazione è vera , restituisce 1 mentre se la relazione è falsa , restituisce 0 .
La tabella seguente riassume gli operatori relazionali utilizzati in C ++.
Operatore | Senso | Esempio |
---|---|---|
== | È uguale a | 3 == 5 ci dà falso |
!= | Non uguale a | 3 != 5 ci dà vero |
> | Più grande di | 3> 5 ci dà falso |
< | Meno di | 3 < 5 ci dà vero |
>= | Maggiore o uguale a | 3>= 5 dacci falso |
<= | Minore o uguale a | 3 <= 5 ci dà vero |
== Operatore
L' ==
operatore uguale a restituisce
true
- se entrambi gli operandi sono uguali o ugualifalse
- se gli operandi sono disuguali
Per esempio,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Nota: l'operatore relazionale ==
non è lo stesso dell'operatore di assegnazione =
. L'operatore di assegnazione =
assegna un valore a una variabile, una costante, una matrice o un vettore. Non confronta due operandi.
! = Operatore
L' !=
operatore non uguale a restituisce
true
- se entrambi gli operandi sono diversifalse
- se entrambi gli operandi sono uguali.
Per esempio,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operatore
L' >
operatore maggiore di restituisce
true
- se l'operando di sinistra è maggiore di quello di destrafalse
- se l'operando sinistro è minore di quello destro
Per esempio,
int x = 10; int y = 15; x> y // false y> x // true
<Operatore
L'operatore minore di <
restituisce
true
- se l'operando sinistro è minore di quello destrofalse
- se l'operando sinistro è maggiore di quello destro
Per esempio,
int x = 10; int y = 15; x < y // true y < x // false
> = Operatore
L' >=
operatore maggiore o uguale a restituisce
true
- se l'operando sinistro è maggiore o uguale a destrafalse
- se l'operando sinistro è minore di quello destro
Per esempio,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operatore
L'operatore minore o uguale a <=
restituisce
true
- se l'operando sinistro è minore o uguale a destrafalse
- se l'operando sinistro è maggiore di quello destro
Per esempio,
int x = 10; int y = 15; x> y // false y> x // true
Per sapere come utilizzare gli operatori relazionali con le stringhe, fare riferimento al nostro tutorial qui.
Operatori logici C ++
Usiamo operatori logici per verificare se un'espressione è vera o falsa . Se l'espressione è vera , restituisce 1 mentre se l'espressione è falsa , restituisce 0 .
Operatore | Esempio | Senso |
---|---|---|
&& | espressione1 && espressione 2 | AND logico. true solo se tutti gli operandi sono veri. |
|| | espressione1 || espressione 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.
Tabella della verità del! Operatore
Sia a un operando. Poi,
Esempio 3: C ++! Operatore
// 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; )
Produzione
1 0
In questo programma, dichiariamo e inizializziamo una int
variabile a con il valore 5
. Quindi stampiamo un'espressione logica
!(a == 0)
Qui, a == 0
restituisce false
come il valore di a 5
. Tuttavia, utilizziamo l'operatore NOT !
su a == 0
. Poiché a == 0
restituisce false
, l' !
operatore inverte i risultati di a == 0
e il risultato finale è true
.
Allo stesso modo, l'espressione alla !(a == 5)
fine ritorna false
perché a == 5
è true
.