V tomto výukovém programu se na příkladech seznámíme s relačními a logickými operátory.
V C ++ relační a logické operátory porovnávají dva nebo více operandů a vracejí buď true
nebo false
hodnoty.
Tyto operátory používáme při rozhodování.
C ++ relační operátoři
Relační operátor se používá ke kontrole vztahu mezi dvěma operandy. Například,
// checks if a is greater than b a> b;
Zde >
je relační operátor. Zkontroluje, zda a je větší než b nebo ne.
Pokud je relace true , vrátí 1, zatímco pokud je relace false , vrátí 0 .
Následující tabulka shrnuje relační operátory používané v C ++.
Operátor | Význam | Příklad |
---|---|---|
== | Je rovný | 3 == 5 dává nám nepravdu |
!= | Nerovná se | 3 != 5 dává nám pravdu |
> | Větší než | 3> 5 dává nám nepravdu |
< | Méně než | 3 < 5 dává nám pravdu |
>= | Větší než nebo rovno | 3>= 5 dej nám faleš |
<= | Méně než nebo rovno | 3 <= 5 dává nám pravdu |
== Provozovatel
Rovná se ==
operátorovi vrátí
true
- pokud jsou oba operandy stejné nebo stejnéfalse
- pokud jsou operandy nerovné
Například,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Poznámka: Relační operátor ==
není stejný jako operátor přiřazení =
. Operátor =
přiřazení přiřadí hodnotu proměnné, konstantě, poli nebo vektoru. Neporovnává dva operandy.
! = Provozovatel
!=
Vrací se nerovná operátorovi
true
- pokud jsou oba operandy nerovnéfalse
- pokud jsou oba operandy stejné.
Například,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Provozovatel
>
Vrací se větší než operátor
true
- pokud je levý operand větší než pravýfalse
- pokud je levý operand menší než pravý
Například,
int x = 10; int y = 15; x> y // false y> x // true
<Provozovatel
Operátor méně než se <
vrátí
true
- pokud je levý operand menší než pravýfalse
- pokud je levý operand větší než pravý
Například,
int x = 10; int y = 15; x < y // true y < x // false
> = Provozovatel
>=
Operátor se vrátí větší nebo rovný
true
- pokud je levý operand větší nebo rovný pravémufalse
- pokud je levý operand menší než pravý
Například,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operátor
<=
Vrátí operátor menší nebo rovný
true
- pokud je levý operand menší nebo rovný pravémufalse
- pokud je levý operand větší než pravý
Například,
int x = 10; int y = 15; x> y // false y> x // true
Pokud se chcete dozvědět, jak lze relační operátory použít s řetězci, přečtěte si náš tutoriál zde.
Logické operátory C ++
Logické operátory používáme ke kontrole, zda je výraz pravdivý nebo nepravdivý . Pokud je výraz pravdivý , vrátí 1, zatímco pokud je výraz nepravdivý , vrátí 0 .
Operátor | Příklad | Význam |
---|---|---|
&& | výraz1 && výraz 2 | Logické AND. true, pouze pokud jsou všechny operandy pravdivé. |
|| | výraz1 || výraz 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á tabulka! Operátor
Nechť být operand. Pak,
Příklad 3: C ++! Operátor
// 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ýstup
1 0
V tomto programu deklarujeme a inicializujeme int
proměnnou a s hodnotou 5
. Potom vytiskneme logický výraz
!(a == 0)
Zde se a == 0
vyhodnotí false
jako hodnota a je 5
. Nicméně, my používáme operátor not !
na a == 0
. Vzhledem a == 0
k tomu false
, že se vyhodnotí , !
operátor převrátí výsledky a == 0
a konečný výsledek je true
.
Podobně se výraz !(a == 5)
nakonec vrátí, false
protože a == 5
je true
.