Logische operatoren in C worden gebruikt om meerdere voorwaarden/beperkingen te combineren. Logische operators retourneren 0 of 1, dit hangt ervan af of het resultaat van de expressie waar of onwaar is. Bij C-programmering voor besluitvorming gebruiken we logische operatoren.
We hebben 3 logische operatoren in de C-taal:
- Logisch AND ( && ) Logisch OR ( || ) Logisch NIET ( !)
Soorten logische operators
1. Logische AND-operator ( && )
Als beide operanden niet nul zijn, wordt de voorwaarde waar. Anders heeft het resultaat de waarde 0. Het retourneringstype van het resultaat is int. Hieronder vindt u de waarheidstabel voor de logische AND-operator.
| X | EN | X && Y |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 normalisatie in de database | 0 | 0 |
Syntaxis
(operand_1 && operand_2)>
Voorbeeld
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;> }> |
>
>Uitvoer
Both values are greater than 0>
2. Logische OR-operator ( || )
De voorwaarde wordt waar als een van deze niet nul is. Anders retourneert het false, d.w.z. 0 als waarde. Hieronder vindt u de waarheidstabel voor de logische OR-operator.
| X | EN | X || EN |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
Syntaxis
(operand_1 || operand_2)>
Voorbeeld
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;> }> |
>
java do while-lus
>Uitvoer
Any one of the given value is greater than 0>
3. Logische NIET-operator (!)
Als de voorwaarde waar is, zal de logische NOT-operator deze onwaar maken en omgekeerd. Hieronder vindt u de waarheidstabel voor de logische NOT-operator.
| X | !X |
|---|---|
| 0 | 1 |
| 1 | 0 |
Syntaxis
! (operand_1 && operand_2)>
Voorbeeld
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;> }> |
>
>
Logische operatoren met kortsluiting
Wanneer het resultaat kan worden bepaald door de voorgaande logische uitdrukking te evalueren zonder de verdere operanden te evalueren, staat dit bekend als kortsluiting.
Kortsluiting is te zien in de vergelijking met meer dan één logische operator. Ze kunnen EN, OF of beide zijn.
1. Kortsluiting in logische AND-operator
De logische AND-operator retourneert waar als en slechts als alle operanden als waar resulteren. Als de eerste operand onwaar is, worden de verdere operanden niet geëvalueerd. Dit komt omdat zelfs als de verdere operanden als waar worden geëvalueerd, de hele voorwaarde nog steeds onwaar zal retourneren.
Voorbeeld
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;> }> |
>
>Uitvoer
Both conditions are satisfied. Conditions not satisfied.>
2. Kortsluiting in logische OK-operator
De OR-operator retourneert waar als ten minste één operand de waarde waar oplevert. Als de eerste operand waar is, worden de verdere operanden niet geëvalueerd. Dit komt omdat zelfs als de verdere operanden als onwaar worden geëvalueerd, de gehele voorwaarde nog steeds waar zal retourneren.
Voorbeeld
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;> }> |
>
>Uitvoer
At least one condition is satisfied. Conditions not satisfied.>
Veelgestelde vragen over logische operators
Q1. Wat is de prioriteit van logische operatoren bij het programmeren?
Antwoord:
De prioriteit van logische operatoren is: NOT, AND, OR. Het wordt echter altijd aanbevolen om haakjes te gebruiken om de volgorde van de evaluatie expliciet te maken en verwarring te voorkomen.
Vraag 2. Kunnen logische operatoren aan elkaar worden gekoppeld?
Antwoord:
Ja, logische operatoren kunnen aan elkaar worden gekoppeld om complexe voorwaarden te creëren. We kunnen bijvoorbeeld meerdere logische AND (&&) of logische OR (||) operatoren combineren in één expressie om meerdere voorwaarden tegelijkertijd te evalueren.
Q3. Wat zal de uitvoer zijn van de volgende code?
C
#include> void> main()> > >int> a = 1, b = 0, c = 5;> >int> d = a && b> |
>
>
Antwoord:
6>
Q4. Wat zal de uitvoer zijn van de volgende code?
C
#include> int> main()> {> >int> i = 1;> >if> (i++ && (i == 1))> >printf>(>'techcodeview.com
'>);> >else> >printf>(>'Coding
'>);> }> |
>
mysql lijst gebruikers
>
Antwoord:
Coding>