Loģiskie operatori C tiek izmantoti, lai apvienotu vairākus nosacījumus/ierobežojumus. Loģiskie operatori atgriež 0 vai 1, tas ir atkarīgs no tā, vai izteiksmes rezultāts ir patiess vai nepatiess. C programmēšanā lēmumu pieņemšanai mēs izmantojam loģiskos operatorus.
Mums ir 3 loģiskie operatori C valodā:
- Loģiskais UN ( && ) Loģiskais VAI ( || ) Loģiskais NAV ( ! )
Loģisko operatoru veidi
1. Loģiskais UN operators (&& )
Ja abi operandi nav nulle, nosacījums kļūst patiess. Pretējā gadījumā rezultāta vērtība ir 0. Rezultāta atgriešanas veids ir int. Zemāk ir patiesības tabula loģiskajam operatoram UN.
| X | UN | X && Y |
|---|---|---|
| 1 | 1 Java apakšvirknes piemērs | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
Sintakse
(operand_1 && operand_2)>
Piemērs
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;> }> |
>
>Izvade
Both values are greater than 0>
2. Loģiskais VAI operators ( || )
Nosacījums kļūst patiess, ja kāds no tiem nav nulle. Pretējā gadījumā tas atgriež false, t.i., 0 kā vērtību. Tālāk ir sniegta loģiskā VAI operatora patiesības tabula.
| X | UN | X || UN |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
Sintakse
(operand_1 || operand_2)>
Piemērs
C
dinamiskais masīvs java
// 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;> }> |
>
>Izvade
Any one of the given value is greater than 0>
3. Loģiskais NOT operators (!)
Ja nosacījums ir patiess, loģiskais operators NOT padarīs to par nepatiesu un otrādi. Zemāk ir patiesības tabula loģiskajam operatoram NOT.
| X | !X |
|---|---|
| 0 | 1 |
| 1 | 0 |
Sintakse
! (operand_1 && operand_2)>
Piemērs
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;> }> |
>
>
Īsslēguma loģiskie operatori
Ja rezultātu var noteikt, novērtējot iepriekšējo loģisko izteiksmi, nenovērtējot turpmākos operandus, to sauc par īssavienojumu.
Īssavienojumu var redzēt vienādojumā, kurā ir vairāk nekā viens loģiskais operators. Tie var būt UN, VAI vai abi.
1. Īssavienojums loģiskajā UN operatorā
Loģiskais operators UN atgriež patiesu tad un tikai tad, ja visi operandi novērtēti kā patiesi. Ja pirmais operands ir nepatiess, tad turpmākie operandi netiks novērtēti. Tas ir tāpēc, ka pat tad, ja turpmākie operandi novērtē kā patiesu, viss nosacījums joprojām atgriezīsies false.
Piemērs
C++
attēls kā fons css
// 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;> }> |
>
>Izvade
Both conditions are satisfied. Conditions not satisfied.>
2. Īssavienojums loģiskajā VAI operatorā
VAI operators atgriež patiesu, ja vismaz viens operands tiek novērtēts kā patiess. Ja pirmais operands ir patiess, tad turpmākie operandi netiks novērtēti. Tas ir tāpēc, ka pat tad, ja turpmākie operandi novērtē kā nepatiesi, viss nosacījums joprojām tiks atgriezts patiess.
Piemērs
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;> }> |
>
>Izvade
At least one condition is satisfied. Conditions not satisfied.>
Bieži uzdotie jautājumi par loģiskajiem operatoriem
Q1. Kāda ir loģisko operatoru prioritāte programmēšanā?
Atbilde:
Loģisko operatoru prioritāte ir: NOT, AND, OR. Tomēr vienmēr ir ieteicams izmantot iekavas, lai vērtēšanas secība būtu skaidra un izvairītos no neskaidrībām.
Q2. Vai loģiskos operatorus var savienot kopā?
Atbilde:
Jā, loģiskos operatorus var savienot kopā, lai radītu sarežģītus apstākļus. Piemēram, mēs varam apvienot vairākus loģiskos UN (&&) vai loģiskos VAI (||) operatorus vienā izteiksmē, lai vienlaikus novērtētu vairākus nosacījumus.
Q3. Kāda būs šāda koda izvade?
C
#include> void> main()> > >int> a = 1, b = 0, c = 5;> >int> d = a && b> |
>
>
Atbilde:
6>
Q4. Kāda būs šāda koda izvade?
C
binārā koka inorder šķērsošana
#include> int> main()> {> >int> i = 1;> >if> (i++ && (i == 1))> >printf>(>'techcodeview.com
'>);> >else> >printf>(>'Coding
'>);> }> |
>
>
Atbilde:
Coding>