C ++ relacijski i logički operatori (s primjerima)

U ovom ćemo tutorijalu uz primjere naučiti o relacijskim i logičkim operatorima.

U C ++-u relacijski i logički operatori uspoređuju dva ili više operanda i vraćaju trueili falsevrijednosti.

Ove operatore koristimo u donošenju odluka.

C ++ relacijski operateri

Relacijski operator koristi se za provjeru odnosa između dva operanda. Na primjer,

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

Ovdje >je relacijski operator. Provjerava je li a veće od b ili nije.

Ako je relacija istinita , vraća 1, a ako je relacija lažna , vraća 0 .

Sljedeća tablica sažima relacijske operatore koji se koriste u C ++.

Operater Značenje Primjer
== Jednako je 3 == 5daje nam lažne
!= Nije jednako 3 != 5daje nam istinu
> Veći od 3> 5daje nam lažne
< Manje od 3 < 5daje nam istinu
>= Veći od ili jednak 3>= 5dajte nam lažno
<= Manje od ili jednako 3 <= 5daje nam istinu

== Operator

==Vraća se jednako operateru

  • true - ako su oba operanda jednaka ili ista
  • false - ako su operandi nejednaki

Na primjer,

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

Napomena: Relacijski operator ==nije isto što i operator dodjele =. Operator dodjele =dodjeljuje vrijednost varijabli, konstanti, nizu ili vektoru. Ne uspoređuje dva operanda.

! = Operater

!=Vraća se operater koji nije jednak

  • true - ako su oba operanda nejednaka
  • false - ako su oba operanda jednaka.

Na primjer,

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

> Operater

>Povrat operatora veći od

  • true - ako je lijevi operand veći od desnog
  • false - ako je lijevi operand manji od desnog

Na primjer,

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

<Operater

<Vraća se operator manje od

  • true - ako je lijevi operand manji od desnog
  • false - ako je lijevi operand veći od desnog

Na primjer,

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

> = Operator

>=Povrat operatora veći ili jednak

  • true - ako je lijevi operand veći ili jednak desnom
  • false - ako je lijevi operand manji od desnog

Na primjer,

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

<= Operator

<=Povrat operatora manji ili jednak

  • true - ako je lijevi operand manji ili jednak desnom
  • false - ako je lijevi operand veći od desnog

Na primjer,

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

Da biste saznali kako se relacijski operatori mogu koristiti sa nizovima, pogledajte naš tutorial ovdje.

Logički operatori C ++

Logičkim operatorima provjeravamo je li izraz istinit ili netačan . Ako je izraz istinit , vraća 1, dok ako je izraz lažan , vraća 0 .

Operater Primjer Značenje
&& izraz1 && izraz 2 Logično I.
true samo ako su svi operandi istiniti.
|| izraz1 || izraz 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.

Tablica istine! Operater

Neka a bude operand. Zatim,

Primjer 3: C ++! Operater

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

Izlaz

 1 0

U ovom programu deklariramo i inicijaliziramo intvarijablu a s vrijednošću 5. Zatim ispisujemo logički izraz

 !(a == 0) 

Ovdje, a == 0procjenjuje falsekao vrijednost a 5. Međutim, mi koristimo NE operatora !na a == 0. Budući da a == 0ocjenjuje na false, !operator invertira rezultate a == 0i konačni rezultat je true.

Slično tome, izraz se na !(a == 5)kraju vraća falsejer a == 5je true.

Zanimljivi članci...