V tej vadnici bomo s pomočjo primerov spoznali relacijske in logične operatorje.
V C ++ relacijski in logični operaterji primerjajo dva ali več operandov in vrnejo true
ali false
vrednosti.
Te operaterje uporabljamo pri odločanju.
Relacijski operaterji C ++
Za preverjanje razmerja med dvema operandoma se uporablja relacijski operator. Na primer
// checks if a is greater than b a> b;
Tukaj >
je relacijski operater. Preveri, ali je a večje od b ali ne.
Če je razmerje true , vrne 1, če pa je razmerje false , vrne 0 .
Naslednja tabela povzema relacijske operatorje, uporabljene v jeziku C ++.
Operater | Pomen | Primer |
---|---|---|
== | Je enako | 3 == 5 nam daje napačno |
!= | Ni enako | 3 != 5 nam daje resnico |
> | Večji kot | 3> 5 nam daje napačno |
< | Manj kot | 3 < 5 nam daje resnico |
>= | Večji ali enak | 3>= 5 daj nam lažno |
<= | Manj ali enako | 3 <= 5 nam daje resnico |
== Operater
==
Vrne se enak operatorju
true
- če sta oba operanda enaka ali enakafalse
- če so operandi neenaki
Na primer
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Opomba: Relacijski operater ==
ni enak operaterju dodelitve =
. Operator dodelitve =
dodeli vrednost spremenljivki, konstanti, matriki ali vektorju. Ne primerja dveh operandov.
! = Operater
!=
Vrne se operator, ki ni enak
true
- če sta oba operanda neenakafalse
- če sta oba operanda enaka.
Na primer
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operater
>
Vrne se operater večji od
true
- če je levi operand večji od desnegafalse
- če je levi operand manjši od desnega
Na primer
int x = 10; int y = 15; x> y // false y> x // true
<Operater
<
Vrne se operator manj kot
true
- če je levi operand manjši od desnegafalse
- če je levi operand večji od desnega
Na primer
int x = 10; int y = 15; x < y // true y < x // false
> = Operater
Vrnitve >=
operaterja večje ali enako
true
- če je levi operand večji ali enak desnemufalse
- če je levi operand manjši od desnega
Na primer
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operater
<=
Vrne se operator, ki je manjši ali enak
true
- če je levi operand manjši ali enak desnemufalse
- če je levi operand večji od desnega
Na primer
int x = 10; int y = 15; x> y // false y> x // true
Če želite izvedeti, kako je mogoče relacijske operatorje uporabljati z nizi, glejte našo vadnico tukaj.
Logični operaterji C ++
Z logičnimi operatorji preverimo, ali je izraz resničen ali neresničen . Če je izraz true , vrne 1, če pa je izraz false , vrne 0 .
Operater | Primer | Pomen |
---|---|---|
&& | IZRAZ1 && izraz 2 | Logično IN. true le, če so vsi operandi resnični. |
|| | izraz1 || izraz 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.
Tabela resnice! Operater
Naj biti operand. Potem,
3. primer: C ++! Operater
// 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; )
Izhod
1 0
V tem programu deklariramo in inicializiramo int
spremenljivko a z vrednostjo 5
. Nato natisnemo logični izraz
!(a == 0)
Tu a == 0
izračuna false
vrednost kot vrednost a 5
. Vendar pa bomo uporabili NE operaterja !
na a == 0
. Ker je a == 0
ovrednoten kot false
je !
operater obrne rezultate a == 0
in končni rezultat je true
.
Podobno se izraz na !(a == 5)
koncu vrne, false
ker a == 5
je true
.