C ++ suhteline ja loogiline operaator (koos näidetega)

Selles õpetuses õpime näidete abil suhtelisi ja loogilisi operaatoreid.

Rakenduses C ++ võrdlevad relatsioonilised ja loogilised operaatorid kahte või enamat operandi ja tagastavad kumbagi truevõi falseväärtused.

Me kasutame neid operaatoreid otsuste tegemisel.

C ++ suhtlusoperaatorid

Relatsioonioperaatorit kasutatakse kahe operandi suhte kontrollimiseks. Näiteks,

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

Siin >on suhteline operaator. See kontrollib, kas a on suurem kui b või mitte.

Kui seos on tõene , tagastab see 1, kui seos on vale , tagastab 0 .

Järgmine tabel võtab kokku C ++ - s kasutatavad seoseoperaatorid.

Operaator Tähendus Näide
== On võrdne 3 == 5annab meile vale
!= Pole võrdne 3 != 5annab meile tõsi
> Suurem kui 3> 5annab meile vale
< Vähem kui 3 < 5annab meile tõsi
>= Suurem või võrdne 3>= 5anna meile vale
<= Vähem kui võrdne 3 <= 5annab meile tõsi

== Operaator

Võrdub ==operaatoriga

  • true - kui mõlemad operandid on võrdsed või samad
  • false - kui operandid on ebavõrdsed

Näiteks,

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

Märkus . Relatsioonioperaator ==ei ole sama mis määranguoperaator =. Omistamise operaator =määrab muutujale, konstandile, massiivile või vektorile väärtuse. Selles ei võrrelda kahte operandi.

! = Operaator

!=Tagastab operaatoriga mitte võrdne

  • true - kui mõlemad operandid on ebavõrdsed
  • false - kui mõlemad operandid on võrdsed.

Näiteks,

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

> Operaator

Suurem kui >operaator tagastab

  • true - kui vasak operand on paremast parem
  • false - kui vasak operand on paremast parem

Näiteks,

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

<Operaator

Vähem kui operaator <tagastab

  • true - kui vasak operand on paremast parem
  • false - kui vasak operand on paremast parem

Näiteks,

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

> = Operaator

Suurem või võrdne >=operaatori tagastusega

  • true - kui vasak operand on paremast parem või sellega võrdne
  • false - kui vasak operand on paremast parem

Näiteks,

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

<= Operaator

Väiksem või võrdne operaator <=tagastab

  • true - kui vasak operand on paremast väiksem või sellega võrdne
  • false - kui vasak operand on paremast parem

Näiteks,

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

Selleks, et teada saada, kuidas relatsioonioperaatoreid saab stringidega kasutada, vaadake meie õpetust siin.

C ++ loogikaoperaatorid

Kasutame loogilisi operaatoreid, et kontrollida, kas avaldis on tõene või väär . Kui avaldis on tõene , tagastab see 1, kui avaldis on vale , tagastab see 0 .

Operaator Näide Tähendus
&& AVALDIS1 && ekspressiooni 2 Loogiline JA.
tõene ainult siis, kui kõik operandid on tõesed.
|| väljend1 || avaldis 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.

Tõe tabel! Operaator

Olgu a operand. Siis,

Näide 3: C ++! Operaator

 // 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äljund

 1 0

Selles programmis deklareerime ja lähtestame intmuutuja a väärtusega 5. Seejärel printime loogilise avaldise

 !(a == 0) 

Siin a == 0hindab falsekui a väärtus on 5. Kuid me kasutame MITTE !kohta a == 0. Kuna a == 0hindab kuni false, !pöörab operaator tulemused a == 0ja lõpptulemus on true.

Samamoodi !(a == 5)naaseb avaldis lõpuks falsesellepärast, et a == 5on true.

Huvitavad Artiklid...