Šioje pamokoje su pavyzdžių pagalba sužinosime apie santykinius ir loginius operatorius.
C ++ sistemoje reliaciniai ir loginiai operatoriai lygina du ar daugiau operandų ir grąžina true
arba false
reikšmes.
Šiuos operatorius naudojame priimdami sprendimus.
C ++ ryšio operatoriai
Reliacinis operatorius naudojamas patikrinti dviejų operandų ryšį. Pavyzdžiui,
// checks if a is greater than b a> b;
Čia >
yra reliacinis operatorius. Jis tikrina, ar a yra didesnis nei b, ar ne.
Jei ryšys teisingas , jis grąžina 1, o jei santykis klaidingas , jis grąžina 0 .
Šioje lentelėje apibendrinti reliaciniai operatoriai, naudojami C ++.
operatorius | Reikšmė | Pavyzdys |
---|---|---|
== | Yra lygus | 3 == 5 suteikia mums klaidingą |
!= | Nelygu | 3 != 5 suteikia mums tikrovę |
> | Geresnis negu | 3> 5 suteikia mums klaidingą |
< | Mažiau nei | 3 < 5 suteikia mums tikrovę |
>= | Didesnis nei lygus | 3>= 5 duok mums klaidingą |
<= | Mažesnis nei lygus | 3 <= 5 suteikia mums tikrovę |
== Operatorius
Lygus ==
operatoriui grįžta
true
- jei abu operandai yra vienodi arba vienodifalse
- jei operandai yra nevienodi
Pavyzdžiui,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Pastaba: Reliacinis operatorius ==
nėra tas pats, kas priskyrimo operatorius =
. Priskyrimo operatorius =
priskiria reikšmę kintamajam, konstantai, masyvui ar vektoriui. Jame nelyginami du operandai.
! = Operatorius
Grąžina ne lygus !=
operatoriui
true
- jei abu operandai yra nevienodifalse
- jei abu operandai yra lygūs.
Pavyzdžiui,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operatorius
Didesnis nei >
operatorius grąžina
true
- jei kairysis operandas yra didesnis už dešinįjįfalse
- jei kairysis operandas yra mažesnis nei dešinysis
Pavyzdžiui,
int x = 10; int y = 15; x> y // false y> x // true
<Operatorius
Kuo mažiau nei operatorius <
grąžina
true
- jei kairysis operandas yra mažesnis nei dešinysisfalse
- jei kairysis operandas yra didesnis nei dešinysis
Pavyzdžiui,
int x = 10; int y = 15; x < y // true y < x // false
> = Operatorius
Didesnis už >=
operatoriaus grąžą arba lygus jam
true
- jei kairysis operandas yra didesnis arba lygus dešiniajamfalse
- jei kairysis operandas yra mažesnis nei dešinysis
Pavyzdžiui,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operatorius
Mažesnis arba lygus operatoriui <=
grąžina
true
- jei kairysis operandas yra mažesnis arba lygus dešiniajamfalse
- jei kairysis operandas yra didesnis nei dešinysis
Pavyzdžiui,
int x = 10; int y = 15; x> y // false y> x // true
Norėdami sužinoti, kaip reliaciniai operatoriai gali būti naudojami su eilutėmis, žiūrėkite mūsų pamoką čia.
C ++ loginiai operatoriai
Mes naudojame loginius operatorius, kad patikrintume, ar išraiška yra teisinga, ar klaidinga . Jei išraiška yra teisinga , ji grąžina 1, o jei išraiška klaidinga , ji grąžina 0 .
operatorius | Pavyzdys | Reikšmė |
---|---|---|
&& | expression1 && išraiška 2 | Loginis IR. tiesa tik tuo atveju, jei visi operandai yra teisingi. |
|| | išraiška1 || 2 išraiška | 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.
Tiesos lentelė! operatorius
Tegul būti operandas. Tada
3 pavyzdys: C ++! operatorius
// 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; )
Rezultatas
1 0
Šioje programoje deklaruojame ir inicializuojame int
kintamąjį a su verte 5
. Tada atspausdiname loginę išraišką
!(a == 0)
Čia a == 0
vertinama false
kaip a vertė yra 5
. Tačiau, mes naudojame 'NE' operatorių !
apie a == 0
. Kadangi a == 0
įvertina iki false
, !
operatorius keičia rezultatus a == 0
ir yra galutinis rezultatas true
.
Panašiai išraiška !(a == 5)
galiausiai grįžta, false
nes a == 5
yra true
.