C ++ relāciju un loģiskie operatori (ar piemēriem)

Šajā apmācībā mēs uzzināsim par relāciju un loģiskajiem operatoriem, izmantojot piemērus.

Programmā C ++ relāciju un loģiskie operatori salīdzina divus vai vairākus operandus un atgriež vai nu truevai falsevērtības.

Mēs šos operatorus izmantojam lēmumu pieņemšanā.

C ++ operāciju operatori

Relāciju operators tiek izmantots, lai pārbaudītu divu operandu attiecības. Piemēram,

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

Šeit >ir relāciju operators. Tas pārbauda, ​​vai a ir lielāks par b vai nav.

Ja saistība ir patiesa , tā atgriež 1, turpretī, ja sakarība ir nepatiesa , tā atgriež 0 .

Šajā tabulā apkopoti relāciju operatori, kas izmantoti C ++.

Operators Nozīme Piemērs
== Ir vienāds ar 3 == 5dod mums nepatiesu
!= Nav vienāds ar 3 != 5dod mums patiesību
> Varenāks, kā 3> 5dod mums nepatiesu
< Mazāk nekā 3 < 5dod mums patiesību
>= Lielāks par vai vienāds ar 3>= 5dod mums nepatiesu
<= Mazāk par vai vienāds ar 3 <= 5dod mums patiesību

== Operators

Atgriežas vienāds ar ==operatoru

  • true - ja abi operandi ir vienādi vai vienādi
  • false - ja operandi ir nevienlīdzīgi

Piemēram,

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

Piezīme . Relāciju operators ==nav tas pats, kas piešķiršanas operators =. Piešķiršanas operators =piešķir vērtību mainīgajam, nemainīgajam, masīvam vai vektoram. Tajā netiek salīdzināti divi operandi.

! = Operators

Atgriežas !=operators, kas nav vienāds ar operatoru

  • true - ja abi operandi ir nevienlīdzīgi
  • false - ja abi operandi ir vienādi.

Piemēram,

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

> Operators

Atgriežas lielāks nekā >operators

  • true - ja kreisais operands ir lielāks par labo
  • false - ja kreisais operands ir mazāks par labo

Piemēram,

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

<Operators

Mazāk nekā operatora <atgriežas

  • true - ja kreisais operands ir mazāks par labo
  • false - ja kreisais operands ir lielāks par labo

Piemēram,

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

> = Operators

Atgriež lielāko vai vienādu ar >=operatoru

  • true - ja kreisais operands ir vai nu lielāks vai vienāds ar labo
  • false - ja kreisais operands ir mazāks par labo

Piemēram,

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

<= Operators

Jo mazāk par vai vienāds ar operatora <=peļņu

  • true - ja kreisais operands ir mazāks vai vienāds ar labo
  • false - ja kreisais operands ir lielāks par labo

Piemēram,

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

Lai uzzinātu, kā relāciju operatorus var izmantot ar virknēm, skatiet mūsu apmācību šeit.

C ++ loģiskie operatori

Mēs izmantojam loģiskos operatorus, lai pārbaudītu, vai izteiksme ir patiesa vai nepatiesa . Ja izteiksme ir patiesa , tā atgriež 1, bet, ja izteiksme ir nepatiesa , atgriež 0 .

Operators Piemērs Nozīme
&& izteiksme1 && izteiksme 2 Loģiski UN.
taisnība tikai tad, ja visi operandi ir patiesi.
|| izteiksme1 || 2. izteiksme 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.

Patiesības tabula! Operators

Ļaut būt operands. Tad,

3. piemērs: C ++! Operators

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

Rezultāts

 1 0

Šajā programmā mēs deklarējam un inicializējam intmainīgo a ar vērtību 5. Pēc tam mēs izdrukājam loģisku izteiksmi

 !(a == 0) 

Šeit a == 0novērtē falsekā vērtību a 5. Taču mēs izmantojam NOT operatoru !par a == 0. Tā kā a == 0novērtē līdz false, !operators apgriež rezultātus a == 0un gala rezultāts ir true.

Līdzīgi izteiciens !(a == 5)galu galā atgriežas, falsejo a == 5ir true.

Interesanti raksti...