I denna handledning lär vi oss om relations- och logiska operatörer med hjälp av exempel.
I C ++ jämför relations- och logiska operatorer två eller flera operander och returnerar endera trueeller falsevärden.
Vi använder dessa operatörer vid beslutsfattande.
C ++ Relationsoperatörer
En relationsoperatör används för att kontrollera förhållandet mellan två operander. Till exempel,
// checks if a is greater than b a> b;
Här >är en relationsoperatör. Den kontrollerar om a är större än b eller inte.
Om förhållandet är sant returnerar det 1 medan om förhållandet är falskt returnerar det 0 .
Följande tabell sammanfattar de relationsoperatorer som används i C ++.
| Operatör | Menande | Exempel |
|---|---|---|
== | Är lika med | 3 == 5ger oss falska |
!= | Inte lika med | 3 != 5ger oss sant |
> | Större än | 3> 5ger oss falska |
< | Mindre än | 3 < 5ger oss sant |
>= | Större än eller lika med | 3>= 5ge oss falska |
<= | Mindre än eller lika med | 3 <= 5ger oss sant |
== Operatör
==Operatörens lika värde
true- om båda operanderna är lika eller sammafalse- om operanderna är ojämlika
Till exempel,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Obs! Relationsoperatören ==är inte densamma som uppdragsoperatören =. Tilldelningsoperatören =tilldelar ett värde till en variabel, konstant, matris eller vektor. Den jämför inte två operander.
! = Operatör
Det som inte är lika med !=operatören returnerar
true- om båda operanderna är ojämlikafalse- om båda operanderna är lika.
Till exempel,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Operatör
Ju större än >operatören returnerar
true- om den vänstra operanden är större än den högrafalse- om den vänstra operanden är mindre än den högra
Till exempel,
int x = 10; int y = 15; x> y // false y> x // true
<Operatör
Den mindre än operatören <återvänder
true- om den vänstra operanden är mindre än den högrafalse- om den vänstra operanden är större än höger
Till exempel,
int x = 10; int y = 15; x < y // true y < x // false
> = Operatör
Större än eller lika med >=operatörens avkastning
true- om den vänstra operanden antingen är större än eller lika med högerfalse- om den vänstra operanden är mindre än den högra
Till exempel,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Operatör
Den mindre än eller lika med operatörens <=avkastning
true- om den vänstra operanden är antingen mindre än eller lika med högerfalse- om den vänstra operanden är större än höger
Till exempel,
int x = 10; int y = 15; x> y // false y> x // true
För att lära oss hur relationsoperatörer kan användas med strängar, se vår handledning här.
C ++ logiska operatörer
Vi använder logiska operatorer för att kontrollera om ett uttryck är sant eller falskt . Om uttrycket är sant returnerar det 1 medan om uttrycket är falskt returnerar det 0 .
| Operatör | Exempel | Menande |
|---|---|---|
&& | expression1 && expression 2 | Logisk OCH. sant endast om alla operander är sanna. |
|| | expression1 || uttryck 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.
Sanningstabellen för! Operatör
Låt a vara en operand. Sedan,
Exempel 3: C ++! Operatör
// 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; )
Produktion
1 0
I detta program deklarerar och initialiserar vi en intvariabel a med värdet 5. Vi skriver sedan ut ett logiskt uttryck
!(a == 0)
Här a == 0utvärderas till falsesom värdet av a är 5. Vi använder dock NOT-operatören !på a == 0. Sedan a == 0utvärderar till false, !inverterar operatören resultaten av a == 0och det slutliga resultatet är true.
På samma sätt !(a == 5)återvänder uttrycket i slutändan falseeftersom det a == 5är true.








