Tässä opetusohjelmassa opitaan relaatio- ja loogisista operaattoreista esimerkkien avulla.
C ++: ssa relaatio- ja loogiset operaattorit vertailevat kahta tai useampaa operandia ja palauttavat joko arvon truetai falsearvon.
Käytämme näitä operaattoreita päätöksenteossa.
C ++ Relaatio-operaattorit
Relaatio-operaattoria käytetään kahden operandin välisen suhteen tarkistamiseen. Esimerkiksi,
// checks if a is greater than b a> b;
Tässä >on relaatiooperaattori. Se tarkistaa, onko a suurempi kuin b vai ei.
Jos suhde on tosi , se palauttaa arvon 1 kun taas suhde on väärä , se palauttaa arvon 0 .
Seuraava taulukko esittää yhteenvedon C ++: ssa käytetyistä relaatio-operaattoreista.
| Operaattori | Tarkoitus | Esimerkki |
|---|---|---|
== | On yhtä suuri kuin | 3 == 5antaa meille väärän |
!= | Ei yhtä suuri kuin | 3 != 5antaa meille totta |
> | Suurempi kuin | 3> 5antaa meille väärän |
< | Vähemmän kuin | 3 < 5antaa meille totta |
>= | Suurempi kuin tai yhtä suuri | 3>= 5anna meille väärä |
<= | Pienempi kuin tai yhtä suuri kuin | 3 <= 5antaa meille totta |
== Operaattori
Equal on ==operaattorin tuottoja
true- jos molemmat operandit ovat samat tai samatfalse- jos operandit ovat eriarvoisia
Esimerkiksi,
int x = 10; int y = 15; int z = 10; x == y // false x == z // true
Huomaa: Relaatio-operaattori ==ei ole sama kuin tehtävänoperaattori =. Määritysoperaattori =määrittää arvon muuttujalle, vakiolle, taulukolle tai vektorille. Siinä ei verrata kahta operandia.
! = Käyttäjä
Ei ole sama kuin !=operaattori palauttaa
true- jos molemmat operandit ovat eriarvoisiafalse- jos molemmat operandit ovat samat.
Esimerkiksi,
int x = 10; int y = 15; int z = 10; x != y // true x != z // false
> Käyttäjä
Suurempi kuin >operaattori palaa
true- jos vasen operandi on suurempi kuin oikeafalse- jos vasen operandi on pienempi kuin oikea
Esimerkiksi,
int x = 10; int y = 15; x> y // false y> x // true
<Käyttäjä
Pienempi kuin operaattori <palaa
true- jos vasen operandi on pienempi kuin oikeafalse- jos vasen operandi on suurempi kuin oikea
Esimerkiksi,
int x = 10; int y = 15; x < y // true y < x // false
> = Käyttäjä
Suurempi tai yhtä suuri kuin >=operaattorin palautus
true- jos vasen operandi on joko suurempi tai yhtä suuri kuin oikeafalse- jos vasen operandi on pienempi kuin oikea
Esimerkiksi,
int x = 10; int y = 15; int z = 10; x>= y // false y>= x // true z>= x // true
<= Käyttäjä
Pienempi tai yhtä suuri kuin operaattori <=palauttaa
true- jos vasen operandi on joko pienempi tai yhtä suuri kuin oikeafalse- jos vasen operandi on suurempi kuin oikea
Esimerkiksi,
int x = 10; int y = 15; x> y // false y> x // true
Jos haluat oppia, kuinka relaatio-operaattoreita voidaan käyttää merkkijonojen kanssa, tutustu oppaamme täällä.
C ++ -loogiset operaattorit
Tarkistamme, onko lauseke tosi vai väärä, loogisten operaattoreiden avulla . Jos lauseke on tosi , se palauttaa arvon 1, kun taas lauseke on epätosi , se palauttaa arvon 0 .
| Operaattori | Esimerkki | Tarkoitus |
|---|---|---|
&& | lauseke1 && ilmaisu 2 | Looginen JA. totta vain, jos kaikki operandit ovat totta. |
|| | lauseke1 || lauseke 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.
Totuuden taulukko! Operaattori
Olkoon a operandi. Sitten,
Esimerkki 3: C ++! Operaattori
// 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; )
Tuotos
1 0
Tässä ohjelmassa ilmoitetaan ja alustetaan intmuuttuja a arvolla 5. Tulostamme sitten loogisen lausekkeen
!(a == 0)
Tässä a == 0arvioidaan niin falsekuin a: n arvo on 5. Käytämme kuitenkin NOT !päälle a == 0. Koska käyttäjä a == 0arvioi false, !operaattori kääntää tulokset a == 0ja lopputulos on true.
Samoin lauseke !(a == 5)palaa viime kädessä falsekoska a == 5on true.








