En este tutorial, aprenderemos sobre operadores lógicos y relacionales con la ayuda de ejemplos.
En C ++, los operadores lógicos y relacionales comparan dos o más operandos y devuelven valores trueo false.
Usamos estos operadores en la toma de decisiones.
Operadores relacionales de C ++
Se utiliza un operador relacional para comprobar la relación entre dos operandos. Por ejemplo,
// checks if a is greater than b a> b;
Aquí, >hay un operador relacional. Comprueba si a es mayor que bo no.
Si la relación es verdadera , devuelve 1, mientras que si la relación es falsa , devuelve 0 .
La siguiente tabla resume los operadores relacionales usados en C ++.
| Operador | Sentido | Ejemplo |
|---|---|---|
== | Es igual a | 3 == 5nos da falso |
!= | No igual a | 3 != 5nos da verdad |
> | Mas grande que | 3> 5nos da falso |
< | Menos que | 3 < 5nos da verdad |
>= | Mayor qué o igual a | 3>= 5danos falso |
<= | Menos que o igual a | 3 <= 5nos da verdad |
== Operador
El ==operador igual a devuelve
true- si ambos operandos son iguales o igualesfalse- si los operandos son desiguales
Por ejemplo,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Nota: El operador relacional ==no es el mismo que el operador de asignación =. El operador de asignación =asigna un valor a una variable, constante, matriz o vector. No compara dos operandos.
! = Operador
El !=operador no igual a devuelve
true- si ambos operandos son desigualesfalse- si ambos operandos son iguales.
Por ejemplo,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operador
El >operador mayor que devuelve
true- si el operando izquierdo es mayor que el derechofalse- si el operando izquierdo es menor que el derecho
Por ejemplo,
int x = 10; int y = 15; x> y // false y> x // true
<Operador
El operador menor que <regresa
true- si el operando izquierdo es menor que el derechofalse- si el operando izquierdo es mayor que el derecho
Por ejemplo,
int x = 10; int y = 15; x < y // true y < x // false
> = Operador
El >=operador mayor o igual que devuelve
true- si el operando izquierdo es mayor o igual que el derechofalse- si el operando izquierdo es menor que el derecho
Por ejemplo,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operador
<=Devuelve el operador menor o igual que
true- si el operando izquierdo es menor o igual que el derechofalse- si el operando izquierdo es mayor que el derecho
Por ejemplo,
int x = 10; int y = 15; x> y // false y> x // true
Para aprender cómo se pueden usar los operadores relacionales con cadenas, consulte nuestro tutorial aquí.
Operadores lógicos C ++
Usamos operadores lógicos para verificar si una expresión es verdadera o falsa . Si la expresión es verdadera , devuelve 1, mientras que si la expresión es falsa , devuelve 0 .
| Operador | Ejemplo | Sentido |
|---|---|---|
&& | expresión1 && expresión 2 | AND lógico. verdadero solo si todos los operandos son verdaderos. |
|| | expresión1 || expresión 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.
Tabla de la verdad del! Operador
Sea a un operando. Entonces,
Ejemplo 3: C ++! Operador
// 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; )
Salida
1 0
En este programa, declaramos e inicializamos una intvariable a con el valor 5. Luego imprimimos una expresión lógica
!(a == 0)
Aquí, se a == 0evalúa falsecomo el valor de a es 5. Sin embargo, usamos el operador NOT !en a == 0. Dado que a == 0evalúa a false, el !operador invierte los resultados de a == 0y el resultado final es true.
De manera similar, la expresión !(a == 5)finalmente devuelve falseporque a == 5es true.








