Tässä osassa käsitellään loogista NOT (!) -operaattoria C-ohjelmointikielessä. Kuten jo tiedämme, loogista operaattoria käytetään loogisen toiminnon suorittamiseen yhdistämällä kaksi tai useampia ehtoja annetuille lausekkeille. Jos operandien loogiset ehdot ovat tosi, operaattori palauttaa todelliset Boolen arvot tai 1. Muussa tapauksessa se palauttaa väärän Boolen arvon tai 0:n. Loogiset operaattorit luokitellaan kolmeen osaan: Looginen JA-, Looginen TAI- ja Looginen EI-operaattorit.
The looginen AND-operaattori käytetään tarkistamaan, että kahden tai useamman jäljellä olevan operandin ehdot ovat tosia tietyssä lausekkeessa; JA-operaattori palauttaa oikean tai nollasta poikkeavan arvon (1). Muussa tapauksessa se palauttaa väärän tai nollan arvon. Voidaan siis sanoa, että looginen AND-operaattori voi toimia vain lausekkeessa, kun molempien operandien ehdot ovat tosi, ja jos jokin ehto ei ole tosi, se palauttaa arvon 0. Looginen AND-operaattori esitetään kaksois-et-symbolina '&&'.
Syntaksi:
(A > b && b > c)
The looginen OR-operaattori käytetään molempien operandien (A ja B) ehtojen tarkistamiseen, ja jos jokin operandeista tai lausekkeista on tosi, operaattori palauttaa todellisen Boolen arvon. Vastaavasti, jos mikään lausekkeista ei ole tosi, se palauttaa arvon epätosi tai nolla. Looginen TAI-operaattori on merkitty kaksoisputkiksi '||' symboli.
Syntaksi:
(A > B) || (A <c) < pre> <h3>Logical NOT operator</h3> <p>The logical NOT operator is represented as the '!' symbol, which is used to reverse the result of any given expression or condition. If the result of an expression is non-zero or true, the result will be reversed as zero or false value. Similarly, if the condition's result is false or 0, the NOT operator reverses the result and returns 1 or true.</p> <p>For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value. And if the user enters a zero (0) value, the operator returns the true Boolean value or 1. </p> <p> <strong>Syntax of the logical NOT operator</strong> </p> <pre> ! (condition); // It '!' represents the NOT operator </pre> <p>Here, the '!' symbol represents the logical NOT operator, which inverses the result of the given condition.</p> <h3>The truth table of the logical NOT operator:</h3> <p>Following is the truth table of the logical not operator in C</p> <pre> condition !(condition) 1 0 0 1 </pre> <h3>Example 1: Program to use the logical NOT operator in C</h3> <p>Let's create a simple program to reverse the given condition of the operands in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (' The return value = %d ', ! (x == 5)); printf (' The return value = %d ', ! (x != 5)); printf (' The return value = %d ', ! (x >= 3)); printf (' The return value = %d ', ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let's create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (' Enter the number: '); scanf ('%d', &x); n = !x; // use logical not operator to reverse the condition printf (' The result of x: %d', n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&&), OR (||), and NOT (!) operator</h3> <p>Let's write a simple program to check whether the given year is a leap or not using the logical AND (&&), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (' Enter the year: '); scanf ('%d', &yr); // use the if..else statement to check the condition /* '&&' (AND) operator to validate both operand, '||' (OR) operator check ny given expressions are true, '!' (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 && yr % 100 != 0)) { printf (' %d is a leap year. ', yr); } else { printf (' %d is not a leap year. ', yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the '2020 is a leap year'.</li> <li>But when we enter the year 2021, it prints the given result '2021 is not a leap year'.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let's write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical 'AND' and logical 'NOT' operator if (a > b && a != 0) { printf (' The AND (&&) operator said: Both conditions are true. '); } // use logical 'OR' and logical 'NOT' operator if (n1 > n2 || n2 != 15) if ( ! (a > b && a != 0 )) { printf (' The NOT (!) operator: Here both conditions are true. '); } else { printf (' The NOT (!) operator: Here, both conditions are true. ' ' But, the status of the condition is reversed as false. '); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&&) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));></pre></c)>
Tässä '!' symboli edustaa loogista NOT-operaattoria, joka kääntää annetun ehdon tuloksen.
Loogisen NOT-operaattorin totuustaulukko:
Seuraavassa on C:n loogisen ei-operaattorin totuustaulukko
condition !(condition) 1 0 0 1
Esimerkki 1: Ohjelma käyttää loogista NOT-operaattoria C:ssä
Tehdään yksinkertainen ohjelma kääntämään C-ohjelmointikielen operandien annetut olosuhteet.
/* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x = 5; // display the result generated using the NOT (!) operator printf (' The return value = %d ', ! (x == 5)); printf (' The return value = %d ', ! (x != 5)); printf (' The return value = %d ', ! (x >= 3)); printf (' The return value = %d ', ! (x <3)); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> The return value = 0 The return value = 1 The return value = 0 The return value = 1 </pre> <p>In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.</p> <h3>Example 2: Program to input a number to perform the logical NOT operator</h3> <p>Let's create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.</p> <pre> /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (' Enter the number: '); scanf ('%d', &x); n = !x; // use logical not operator to reverse the condition printf (' The result of x: %d', n); // display the result return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the number: 7 The result of x: 0 </pre> <p>In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.</p> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the number: 0 The result of x: 1 </pre> <p>Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.</p> <h3>Example 3: Program to find the leap year using the logical AND (&&), OR (||), and NOT (!) operator</h3> <p>Let's write a simple program to check whether the given year is a leap or not using the logical AND (&&), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (' Enter the year: '); scanf ('%d', &yr); // use the if..else statement to check the condition /* '&&' (AND) operator to validate both operand, '||' (OR) operator check ny given expressions are true, '!' (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 && yr % 100 != 0)) { printf (' %d is a leap year. ', yr); } else { printf (' %d is not a leap year. ', yr); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Enter the year: 2020 2020 is a leap year. </pre> <p>In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;</p> <ol class="points"> <li>The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.</li> <li>In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the '2020 is a leap year'.</li> <li>But when we enter the year 2021, it prints the given result '2021 is not a leap year'.</li> </ol> <p> <strong>2<sup>nd</sup> execution:</strong> </p> <pre> Enter the year: 2021 2021 is not a leap year. </pre> <p> <strong>Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator</strong> </p> <p>Let's write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.</p> <pre> /* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical 'AND' and logical 'NOT' operator if (a > b && a != 0) { printf (' The AND (&&) operator said: Both conditions are true. '); } // use logical 'OR' and logical 'NOT' operator if (n1 > n2 || n2 != 15) if ( ! (a > b && a != 0 )) { printf (' The NOT (!) operator: Here both conditions are true. '); } else { printf (' The NOT (!) operator: Here, both conditions are true. ' ' But, the status of the condition is reversed as false. '); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&&) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false. </pre> <hr></3));>
Yllä olevassa ohjelmassa NOT (!) -operaattorilla käännetään eri lausekkeiden tulos, esimerkiksi muuttujan x ehto on 5, mikä on totta. Silti NOT-operaattori kääntää tuloksen ja palauttaa 0. Samoin määritimme ehdon (x!=5), mutta looginen operaattori muutti tulostaan ja palautti 1 ja niin edelleen.
Esimerkki 2: Ohjelma syöttää numero loogisen NOT-operaattorin suorittamiseksi
Luodaan yksinkertainen ohjelma kokonaisluvun käänteisen tuloksen saamiseksi käyttämällä C-ohjelmointikielen loogista NOT (!) -operaattoria.
/* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (' Enter the number: '); scanf ('%d', &x); n = !x; // use logical not operator to reverse the condition printf (' The result of x: %d', n); // display the result return 0; }
Lähtö:
Enter the number: 7 The result of x: 0
Yllä olevassa ohjelmassa syötetään käyttäjältä kokonaisluku 7 ja tallennetaan se x-muuttujaan. Tämän jälkeen looginen EI (!) -operaattori kääntää x:n arvon (ei nolla) ja palauttaa nollan (0) tulostaakseen x:n tuloksen.
2ndtoteutus:
Enter the number: 0 The result of x: 1
Vastaavasti syötämme käyttäjältä nollan (0) ja käytämme loogista NOT (!) -operaattoria kääntämään x:n arvon nollasta poikkeavaksi arvoksi, joka on 1.
Esimerkki 3: Ohjelma karkausvuoden etsimiseen käyttämällä loogista AND (&&), OR (||) ja NOT (!) -operaattoria
Kirjoitetaan yksinkertainen ohjelma, jolla tarkistetaan, onko annettu vuosi harppaus vai ei, käyttämällä C-kielen loogista AND (&&), loogista TAI (||) ja loogista EI (!) -operaattoria.
#include #include int main () { int yr; // declare int type variable printf (' Enter the year: '); scanf ('%d', &yr); // use the if..else statement to check the condition /* '&&' (AND) operator to validate both operand, '||' (OR) operator check ny given expressions are true, '!' (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 && yr % 100 != 0)) { printf (' %d is a leap year. ', yr); } else { printf (' %d is not a leap year. ', yr); } return 0; }
Lähtö:
Enter the year: 2020 2020 is a leap year.
Yllä olevassa ohjelmassa syötetään vuosi 2020 ja sitten tarkistetaan annettu vuosi määrittämällä if...else lauseke. Tässä lausunnossa määritimme kaksi ehtoa;
- Annettu vuosi jaetaan 400:lla, mikä on yhtä kuin 0. Ja sitten käytämme loogista OR-operaattoria tarkistaaksemme, onko vasen vai oikea operandiehto tosi.
- Toisessa ehdossa annettu vuosi jaetaan 4:llä ja 100:lla. Mutta kun jaamme vuoden 2020 4:llä, mikä on yhtä kuin 0. Samoin jaamme vuoden 2020 100:lla, mikä ei myöskään ole 0. Joten molemmat ehdot ovat totta, että '2020 on karkausvuosi' näytetään.
- Mutta kun astumme vuoteen 2021, se tulostaa annetun tuloksen '2021 ei ole karkausvuosi'.
2ndtoteutus:
Enter the year: 2021 2021 is not a leap year.
Esimerkki 4: Ohjelma tarkistaa eri ehdot käyttämällä AND-, OR- ja NOT-operaattoria
nfa esimerkkejä
Kirjoitetaan ohjelma, joka osoittaa annettujen operandien useat ehdot käyttämällä AND-, OR- ja NOT-loogista operaattoria C:ssä.
/* program to check the various condition using the logical NOT operator in c. */ #include #include int main () { // declare and initialize variables int a = 20, b = 15; int n1 = 15, n2 = 17; // use logical 'AND' and logical 'NOT' operator if (a > b && a != 0) { printf (' The AND (&&) operator said: Both conditions are true. '); } // use logical 'OR' and logical 'NOT' operator if (n1 > n2 || n2 != 15) if ( ! (a > b && a != 0 )) { printf (' The NOT (!) operator: Here both conditions are true. '); } else { printf (' The NOT (!) operator: Here, both conditions are true. ' ' But, the status of the condition is reversed as false. '); } return 0; }
Lähtö:
The AND (&&) operator is said: Both conditions are true. The OR (||) operator is said: Only one condition is true. The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false.
3));>