Loginiai operatoriai C yra naudojami kelioms sąlygoms / apribojimams sujungti. Loginiai operatoriai grąžina 0 arba 1, tai priklauso nuo to, ar išraiškos rezultatas yra teisingas ar klaidingas. C programavimuose sprendimų priėmimui naudojame loginius operatorius.
usa kiek miestų
Mes turime 3 loginius operatorius C kalba:
- Loginis IR ( && ) Loginis ARBA ( || ) Loginis NE ( ! )
Loginių operatorių tipai
1. Loginis IR operatorius ( && )
Jei abu operandai nėra nuliai, sąlyga tampa teisinga. Kitu atveju rezultato reikšmė yra 0. Rezultato grąžinimo tipas yra int. Žemiau yra loginio AND operatoriaus tiesos lentelė.
X | IR | X & & Y |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Sintaksė
(operand_1 && operand_2)>
Pavyzdys
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;> }> |
>
>Išvestis
Both values are greater than 0>
2. Loginis ARBA operatorius ( || )
Sąlyga tampa teisinga, jei kuri nors iš jų yra ne nulis. Kitu atveju jis grąžina false, ty 0 kaip reikšmę. Žemiau yra loginio ARBA operatoriaus tiesos lentelė.
X | IR | X || IR |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Sintaksė
(operand_1 || operand_2)>
Pavyzdys
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;> }> |
>
>Išvestis
sąjunga prieš sąjungą visi
Any one of the given value is greater than 0>
3. Loginis NE operatorius (!)
Jei sąlyga teisinga, loginis operatorius NOT padarys ją klaidinga ir atvirkščiai. Žemiau yra loginio NOT operatoriaus tiesos lentelė.
X | !X |
---|---|
0 | 1 |
1 | 0 |
Sintaksė
! (operand_1 && operand_2)>
Pavyzdys
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;> }> |
>
>
Trumpojo jungimo loginiai operatoriai
Kai rezultatą galima nustatyti įvertinus ankstesnę loginę išraišką, neįvertinus tolesnių operandų, tai vadinama trumpuoju jungimu.
Trumpasis jungimas gali būti matomas lygtyje, turinčioje daugiau nei vieną loginį operatorių. Jie gali būti IR, ARBA arba abu.
1. Trumpasis jungimas loginiame IR operatoriuje
Loginis operatorius IR grąžina teisingą tada ir tik tada, kai visi operandai vertinami kaip tiesa. Jei pirmasis operandas klaidingas, kiti operandai nebus vertinami. Taip yra todėl, kad net jei kiti operandai įvertins teisingą, visa sąlyga vis tiek bus klaidinga.
Pavyzdys
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;> }> |
>
>Išvestis
Both conditions are satisfied. Conditions not satisfied.>
2. Trumpasis jungimas loginiame ARBA operatoriuje
ARBA operatorius grąžina tiesa, jei bent vienas operandas įvertina teisingą. Jei pirmasis operandas yra teisingas, kiti operandai nebus vertinami. Taip yra todėl, kad net jei kiti operandai įvertins klaidingą, visa sąlyga vis tiek bus teisinga.
Pavyzdys
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;> }> |
>
jei dar sakinys java
>Išvestis
At least one condition is satisfied. Conditions not satisfied.>
DUK apie loginius operatorius
Q1. Kokia yra loginių operatorių pirmenybė programuojant?
Atsakymas:
Loginių operatorių pirmenybė yra: NOT, AND, OR. Tačiau visada rekomenduojama naudoti skliaustus, kad vertinimo tvarka būtų aiški ir išvengta painiavos.
Q2. Ar loginius operatorius galima sujungti grandinėmis?
Atsakymas:
Taip, loginius operatorius galima sujungti grandinėmis, kad būtų sukurtos sudėtingos sąlygos. Pavyzdžiui, vienoje išraiškoje galime sujungti kelis loginius IR (&&) arba loginius ARBA (||) operatorius, kad vienu metu įvertintume kelias sąlygas.
Q3. Kokia bus šio kodo išvestis?
C
#include> void> main()> > > int> a = 1, b = 0, c = 5;> > int> d = a && b> |
>
>
Atsakymas:
6>
4 klausimas. Kokia bus šio kodo išvestis?
C
#include> int> main()> {> > int> i = 1;> > if> (i++ && (i == 1))> > printf> (> 'techcodeview.com
'> );> > else> > printf> (> 'Coding
'> );> }> |
>
>
Atsakymas:
Coding>