V tomto tutoriále sa pomocou príkladov dozvieme o relačných a logických operátoroch.
V C ++ porovnávajú relačné a logické operátory dva alebo viac operandov a vracajú buď true
alebo false
hodnoty.
Tieto subjekty používame pri rozhodovaní.
C ++ relačné operátory
Relačný operátor sa používa na kontrolu vzťahu medzi dvoma operandmi. Napríklad,
// checks if a is greater than b a> b;
Tu >
je relačný operátor. Kontroluje, či je a väčšie ako b alebo nie.
Ak je vzťah pravdivý , vráti hodnotu 1, zatiaľ čo ak je vzťah nepravdivý , vráti hodnotu 0 .
Nasledujúca tabuľka sumarizuje relačné operátory použité v C ++.
Prevádzkovateľ | Význam | Príklad |
---|---|---|
== | Rovná sa | 3 == 5 nám dáva nepravdu |
!= | Nerovná sa | 3 != 5 dáva nám pravdu |
> | Väčší než | 3> 5 nám dáva nepravdu |
< | Menej ako | 3 < 5 dáva nám pravdu |
>= | Väčšie alebo rovnaké | 3>= 5 daj nám nepravdu |
<= | Menej ako alebo rovnaké | 3 <= 5 dáva nám pravdu |
== Prevádzkovateľ
Rovná sa ==
operátorovi
true
- ak sú oba operandy rovnaké alebo rovnakéfalse
- ak sú operandy nerovné
Napríklad,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Poznámka: Relačný operátor ==
nie je rovnaký ako operátor priradenia =
. Operátor priradenia =
priradí hodnotu premennej, konštante, poľu alebo vektoru. Neporovnáva dva operandy.
! = Prevádzkovateľ
Hodnota nerovná sa !=
operátorovi sa vráti
true
- ak sú oba operandy nerovnakéfalse
- ak sú obidva operandy rovnaké.
Napríklad,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Prevádzkovateľ
Čím viac sa >
vráti operátor
true
- ak je ľavý operand väčší ako pravýfalse
- ak je ľavý operand menší ako pravý
Napríklad,
int x = 10; int y = 15; x> y // false y> x // true
<Prevádzkovateľ
Operátor menej ako sa <
vráti
true
- ak je ľavý operand menší ako pravýfalse
- ak je ľavý operand väčší ako pravý
Napríklad,
int x = 10; int y = 15; x < y // true y < x // false
> = Prevádzkovateľ
Čím väčšia alebo rovná sa >=
návratnosť operátora
true
- ak je ľavý operand väčší alebo rovný pravémufalse
- ak je ľavý operand menší ako pravý
Napríklad,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operátor
The less than or equal to operator <=
returns
true
- if the left operand is either less than or equal to the rightfalse
- if the left operand is greater than right
For example,
int x = 10; int y = 15; x> y // false y> x // true
In order to learn how relational operators can be used with strings, refer to our tutorial here.
C++ Logical Operators
We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator | Example | Meaning |
---|---|---|
&& | expression1 && expression 2 | Logical AND. true only if all the operands are true. |
|| | expression1 || expression 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.
Pravdivá tabuľka! Prevádzkovateľ
Nech byť operand. Potom,
Príklad 3: C ++! Prevádzkovateľ
// 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; )
Výkon
1 0
V tomto programe deklarujeme a inicializujeme int
premennú a s hodnotou 5
. Potom vytlačíme logický výraz
!(a == 0)
Tu sa a == 0
vyhodnotí false
ako hodnota a 5
. Avšak, my používame operátor nôt !
na a == 0
. Od a == 0
vyhodnotenia do false
, !
operátor invertuje výsledky a == 0
a konečný výsledok je true
.
Podobne sa !(a == 5)
nakoniec vráti výraz, false
pretože a == 5
je true
.