Operatori relaționali și logici C ++ (cu exemple)

În acest tutorial, vom învăța despre operatorii relaționali și logici cu ajutorul exemplelor.

În C ++, operatorii relaționali și logici compară doi sau mai mulți operanzi și returnează fie truesau falsevalori.

Folosim acești operatori în luarea deciziilor.

Operatori relaționali C ++

Un operator relațional este utilizat pentru a verifica relația dintre doi operanzi. De exemplu,

 // checks if a is greater than b a> b;

Iată >un operator relațional. Se verifică dacă a este mai mare decât b sau nu.

Dacă relația este adevărată , returnează 1, în timp ce dacă relația este falsă , returnează 0 .

Tabelul următor rezumă operatorii relaționali utilizați în C ++.

Operator Sens Exemplu
== Este egal cu 3 == 5ne dă fals
!= Nu este egal cu 3 != 5ne dă adevărat
> Mai mare ca 3> 5ne dă fals
< Mai puțin decât 3 < 5ne dă adevărat
>= Mai mare sau egal cu 3>= 5dă-ne fals
<= Mai mic sau egal cu 3 <= 5ne dă adevărat

== Operator

Se ==întoarce egal cu operatorul

  • true - dacă ambii operanzi sunt egali sau la fel
  • false - dacă operanzii sunt inegali

De exemplu,

 int x = 10; int y = 15; int z = 10; x == y // false x == z // true

Notă: Operatorul relațional ==nu este același cu operatorul de atribuire =. Operatorul de atribuire =atribuie o valoare unei variabile, constantei, matricei sau vectorului. Nu compară doi operanzi.

! = Operator

!=Întoarcerea care nu este egală cu operatorul

  • true - dacă ambii operanzi sunt inegali
  • false - dacă ambii operanzi sunt egali.

De exemplu,

 int x = 10; int y = 15; int z = 10; x != y // true x != z // false

> Operator

>Revine cu cel mai mare decât operatorul

  • true - dacă operandul din stânga este mai mare decât cel din dreapta
  • false - dacă operandul din stânga este mai mic decât cel din dreapta

De exemplu,

 int x = 10; int y = 15; x> y // false y> x // true

<Operator

<Revine operatorul mai puțin decât

  • true - dacă operandul din stânga este mai mic decât cel din dreapta
  • false - dacă operandul stâng este mai mare decât dreapta

De exemplu,

 int x = 10; int y = 15; x < y // true y < x // false

> = Operator

Se >=întoarce cel mai mare sau egal cu operatorul

  • true - dacă operandul din stânga este mai mare sau egal cu dreapta
  • false - dacă operandul din stânga este mai mic decât cel din dreapta

De exemplu,

 int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true

<= Operator

<=Revine operatorul mai mic sau egal cu

  • true - dacă operandul stâng este fie mai mic, fie egal cu dreapta
  • false - dacă operandul stâng este mai mare decât dreapta

De exemplu,

 int x = 10; int y = 15; x> y // false y> x // true

Pentru a afla cum pot fi utilizați operatorii relaționali cu șirurile, consultați tutorialul nostru aici.

Operatori logici C ++

Folosim operatori logici pentru a verifica dacă o expresie este adevărată sau falsă . Dacă expresia este adevărată , returnează 1, în timp ce dacă expresia este falsă , returnează 0 .

Operator Exemplu Sens
&& expresie1 && expresie 2 Logic ȘI.
adevărat numai dacă toți operanzii sunt adevărați.
|| expresie1 || expresia 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 are true.
  • false - if one or more operands are false.

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 are true.
  • false - if and only if all the operands are false.

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.

Tabelul Adevărului! Operator

o să fie un operand. Apoi,

Exemplul 3: C ++! Operator

 // 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; )

Ieșire

 1 0

În acest program, declarăm și inițializăm o intvariabilă a cu valoarea 5. Apoi imprimăm o expresie logică

 !(a == 0) 

Aici, se a == 0evaluează falsedupă cum este valoarea unui 5. Cu toate acestea, folosim operatorul NOT !pornit a == 0. Deoarece a == 0evaluează la false, !operatorul inversează rezultatele a == 0și rezultatul final este true.

În mod similar, expresia !(a == 5)revine în cele din urmă falsepentru că a == 5este true.

Articole interesante...