logo

C Loogiset operaattorit

C:n loogisia operaattoreita käytetään useiden ehtojen/rajoitusten yhdistämiseen. Loogiset operaattorit palauttaa joko 0 tai 1, se riippuu siitä, onko lausekkeen tulos tosi vai epätosi. C-ohjelmoinnissa päätöksenteossa käytämme loogisia operaattoreita.

Meillä on 3 loogista operaattoria C-kielellä:



git-komennot pushille
    Looginen JA ( && ) Looginen TAI ( || ) Looginen EI ( ! )

Loogisten operaattoreiden tyypit

1. Looginen AND-operaattori ( && )

Jos molemmat operandit eivät ole nollia, ehdosta tulee tosi. Muussa tapauksessa tuloksen arvo on 0. Tuloksen palautustyyppi on int. Alla on loogisen AND-operaattorin totuustaulukko.

X

JA X && Y

1



1

1

1



0

0

0

1

0

0

0

0

Syntaksi

(operand_1 && operand_2)>

Esimerkki

C




// C program for Logical> // AND Operator> #include> // Driver code> int> main()> {> >int> a = 10, b = 20;> >if> (a>0 && b> 0) {> >printf>(>'Both values are greater than 0 '>);> >}> >else> {> >printf>(>'Both values are less than 0 '>);> >}> >return> 0;> }>

>

>

Lähtö

Both values are greater than 0>

2. Looginen TAI -operaattori ( || )

Ehto toteutuu, jos jokin niistä on nollasta poikkeava. Muussa tapauksessa se palauttaa arvoksi false eli 0:n. Alla on loogisen TAI-operaattorin totuustaulukko.

X JA X || JA

1

1

1

1

0

kokeile catch block java

1

0

1

1

0

0

0

Syntaksi

(operand_1 || operand_2)>

Esimerkki

C




// C program for Logical> // OR Operator> #include> // Driver code> int> main()> {> >int> a = -1, b = 20;> >if> (a>0 || b> 0) {> >printf>(>'Any one of the given value is '> >'greater than 0 '>);> >}> >else> {> >printf>(>'Both values are less than 0 '>);> >}> >return> 0;> }>

>

>

Lähtö

Any one of the given value is greater than 0>

3. Looginen EI-operaattori (!)

Jos ehto on tosi, looginen EI-operaattori tekee siitä epätosi ja päinvastoin. Alla on totuustaulukko loogiselle NOT-operaattorille.

X !X

0

1

1

0

Syntaksi

 ! (operand_1 && operand_2)>

Esimerkki

C




// C program for Logical> // NOT Operator> #include> // Driver code> int> main()> {> >int> a = 10, b = 20;> >if> (!(a>0 && b> 0)) {> >// condition returned true but> >// logical NOT operator changed> >// it to false> >printf>(>'Both values are greater than 0 '>);> >}> >else> {> >printf>(>'Both values are less than 0 '>);> >}> >return> 0;> }>

>

java-muuttujamuuttuja

>

Oikosulkuloogiset operaattorit

Kun tulos voidaan määrittää arvioimalla edellinen Looginen lauseke ilman muita operandeja, sitä kutsutaan oikosulkuksi.

Oikosulku voidaan nähdä yhtälössä, jossa on useampi kuin yksi looginen operaattori. Ne voivat olla joko AND, OR tai molemmat.

1. Oikosulku loogisessa AND operaattorissa

Looginen AND-operaattori palauttaa tosi, jos ja vain jos kaikki operandit arvioivat tosi. Jos ensimmäinen operandi on epätosi, muita operandeja ei arvioida. Tämä johtuu siitä, että vaikka muut operandit arvioisivat todeksi, koko ehto palauttaa silti epätosi.

Esimerkki

C++




// C++ Program to illustrate short circuiting in Logical AND> #include> using> namespace> std;> // utility function to check positive> bool> is_positive(>int> number)> {> >if> (number>0)> >return> true>;> >else> >return> false>;> }> // utility function to check if the number is even> bool> is_even(>int> number)> {> >if> (number % 2 == 0)> >return> true>;> >else> >return> false>;> }> // driver code> int> main()> {> >int> x = 10;> >// Both conditions are evaluated> >if> (is_positive(x) && is_even(x)) {> >cout <<>'Both conditions are satisfied.'> << endl;> >}> >else> {> >cout <<>'Conditions not satisfied.'> << endl;> >}> >int> y = -5;> >// The first condition is evaluated and found to be> >// false, so the second condition is not evaluated> >if> (is_positive(y) && is_even(y)) {> >cout <<>'Both conditions are satisfied.'> << endl;> >}> >else> {> >cout <<>'Conditions not satisfied.'> << endl;> >}> >return> 0;> }>

>

>

Lähtö

Both conditions are satisfied. Conditions not satisfied.>

2. Oikosulku loogisessa TAI operaattorissa

OR-operaattori palauttaa tosi, jos ainakin yksi operandi arvioi arvoon tosi. Jos ensimmäinen operandi on tosi, muita operandeja ei arvioida. Tämä johtuu siitä, että vaikka muut operandit arvioisivat epätosi, koko ehto palauttaa silti tosi.

Esimerkki

C++




// C++ program to illustrate the short circuiting in Logical> // OR> #include> using> namespace> std;> // utility function to check positive number> bool> is_positive(>int> number)> {> >if> (number>0)> >return> true>;> >else> >return> false>;> }> // utility function to check if the number is even> bool> is_even(>int> number)> {> >if> (number % 2 == 0)> >return> true>;> >else> >return> false>;> }> // driver code> int> main()> {> >int> x = 8;> >// The first condition is evaluated and found to be> >// true, so the second condition is not evaluated> >if> (is_positive(x) || is_even(x)) {> >cout <<>'At least one condition is satisfied.'> ><< endl;> >}> >else> {> >cout <<>'Conditions not satisfied.'> << endl;> >}> >int> y = -5;> >// The first condition is evaluated and found to be> >// false, so the second condition is evaluated> >if> (is_positive(y) || is_even(y)) {> >cout <<>'At least one condition is satisfied.'> ><< endl;> >}> >else> {> >cout <<>'Conditions not satisfied.'> << endl;> >}> >return> 0;> }>

>

>

Lähtö

At least one condition is satisfied. Conditions not satisfied.>

Usein kysytyt kysymykset loogisista operaattoreista

Q1. Mikä on loogisten operaattoreiden etusija ohjelmoinnissa?

Vastaus:

Loogisten operaattoreiden ensisijaisuus on: NOT, AND, OR. On kuitenkin aina suositeltavaa käyttää sulkuja, jotta arviointijärjestys selvennetään ja vältetään sekaannukset.

Q2. Voidaanko loogisia operaattoreita ketjuttaa yhteen?

Vastaus:

Kyllä, loogiset operaattorit voidaan ketjuttaa yhteen monimutkaisten olosuhteiden luomiseksi. Voimme esimerkiksi yhdistää useita loogisia AND (&&) tai loogisia OR (||) -operaattoreita yhdeksi lausekkeeksi arvioidaksemme useita ehtoja samanaikaisesti.

Q3. Mikä on seuraavan koodin tulos?

C




#include> void> main()> > >int> a = 1, b = 0, c = 5;> >int> d = a && b>

>

java on yhtä suuri

>

Vastaus:

6>

Q4. Mikä on seuraavan koodin tulos?

C




#include> int> main()> {> >int> i = 1;> >if> (i++ && (i == 1))> >printf>(>'techcodeview.com '>);> >else> >printf>(>'Coding '>);> }>

>

>

Vastaus:

Coding>