logo

C'de Mantıksal NOT (!) Operatörü

Bu bölümde C programlama dilinde mantıksal NOT(!) operatörü tartışılacaktır. Zaten bildiğimiz gibi mantıksal operatör, verilen ifadelerde iki veya daha fazla koşulu birleştirerek mantıksal işlemi gerçekleştirmek için kullanılır. İşlenenlerin mantıksal koşulları doğruysa, operatör doğru Boolean değerlerini veya 1'i döndürür. Aksi takdirde, yanlış Boolean değerini veya 0'ı döndürür. Mantıksal operatörler üç bölüme ayrılır: Mantıksal VE, Mantıksal VEYA ve Mantıksal DEĞİL operatörleri.

C'de Mantıksal NOT (!) Operatörü

mantıksal VE operatörü belirli bir ifadede kalan iki veya daha fazla işlenenin koşullarının doğru olup olmadığını kontrol etmek için kullanılır; AND operatörü doğru veya sıfır olmayan (1) bir değer döndürür. Aksi halde false veya 0 değeri döndürür. Yani mantıksal AND operatörünün ancak her iki işlenenin de koşulları doğru olduğunda bir ifadede işlem yapabileceğini ve herhangi bir koşul doğru değilse 0 değerini döndürdüğünü söyleyebiliriz. Mantıksal AND operatörü çift ve işareti '&&' simgesiyle temsil edilir.

int'yi double java'ya dönüştür

Sözdizimi:

 (A > b && b > c) 

mantıksal OR operatörü her iki işlenenin (A ve B) koşullarını kontrol etmek için kullanılır ve işlenenlerden veya ifadelerden biri doğruysa operatör gerçek bir Boolean değeri döndürür. Benzer şekilde ifadelerden hiçbiri doğru değilse yanlış veya sıfır değeri döndürür. Mantıksal VEYA operatörü çift boru '||' olarak gösterilir sembol.

Sözdizimi:

 (A &gt; B) || (A <c) < pre> <h3>Logical NOT operator</h3> <p>The logical NOT operator is represented as the &apos;!&apos; 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&apos;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 &apos;!&apos; represents the NOT operator </pre> <p>Here, the &apos;!&apos; 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&apos;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 (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (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&apos;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 (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, 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 (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, 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 &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</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&apos;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 &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) 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)>

Burada '!' sembolü, verilen koşulun sonucunu tersine çeviren mantıksal DEĞİL operatörünü temsil eder.

Mantıksal DEĞİL operatörünün doğruluk tablosu:

Aşağıda C'deki mantıksal değil operatörünün doğruluk tablosu verilmiştir.

 condition !(condition) 1 0 0 1 

Örnek 1: C'de mantıksal NOT operatörünü kullanan program

C programlama dilinde işlenenlerin verilen durumunu tersine çevirecek basit bir program oluşturalım.

 /* 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 (&apos; The return value = %d 
&apos;, ! (x == 5)); printf (&apos; The return value = %d 
&apos;, ! (x != 5)); printf (&apos; The return value = %d 
&apos;, ! (x &gt;= 3)); printf (&apos; The return value = %d 
&apos;, ! (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&apos;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 (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, 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 (&amp;&amp;), OR (||), and NOT (!) operator</h3> <p>Let&apos;s write a simple program to check whether the given year is a leap or not using the logical AND (&amp;&amp;), logical OR (||), and the logical NOT (!) operator in the C language.</p> <pre> #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, 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 &apos;2020 is a leap year&apos;.</li> <li>But when we enter the year 2021, it prints the given result &apos;2021 is not a leap year&apos;.</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&apos;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 &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> The AND (&amp;&amp;) 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));>

Yukarıdaki programda, x değişkeninin 5'e eşit olması durumu gibi çeşitli ifadelerin sonucunu tersine çevirmek için NOT (!) operatörünü kullanırız ki bu doğrudur. Yine de NOT operatörü sonucu tersine çevirir ve 0 döndürür. Benzer şekilde (x!=5) koşulunu da tanımladık, ancak mantıksal operatör sonucunu değiştirdi ve 1 döndürdü ve bu böyle devam etti.

Örnek 2: Mantıksal DEĞİL operatörünü gerçekleştirmek için bir sayı girecek program

C programlama dilinde mantıksal NOT(!) operatörünü kullanarak bir tam sayının ters sonucunu elde eden basit bir program oluşturalım.

 /* demonstration the use of the logical not operator in C. */ #include #include int main () { // declare an initialize x variable int x, n; printf (&apos; Enter the number: &apos;); scanf (&apos;%d&apos;, &amp;x); n = !x; // use logical not operator to reverse the condition printf (&apos; The result of x: %d&apos;, n); // display the result return 0; } 

Çıktı:

 Enter the number: 7 The result of x: 0 

Yukarıdaki programda kullanıcıdan gelen 7 tamsayısını girip x değişkenine saklıyoruz. Bundan sonra mantıksal NOT(!) operatörü x'in değerini (sıfır olmayan) tersine çevirir ve x'in sonucunu yazdırmak için sıfır (0) döndürür.

2veuygulamak:

java kırpma dizesi
 Enter the number: 0 The result of x: 1 

Benzer şekilde, kullanıcıdan sıfır (0) gireriz ve x'in değerini sıfır olmayan bir değere, yani 1'e tersine çevirmek için mantıksal NOT(!) operatörünü kullanırız.

Örnek 3: Mantıksal AND (&&), OR (||) ve NOT (!) operatörünü kullanarak artık yılı bulan program

C dilinde mantıksal AND (&&), mantıksal OR (||) ve mantıksal NOT (!) operatörünü kullanarak verilen yılın artık olup olmadığını kontrol eden basit bir program yazalım.

 #include #include int main () { int yr; // declare int type variable printf (&apos; Enter the year: &apos;); scanf (&apos;%d&apos;, &amp;yr); // use the if..else statement to check the condition /* &apos;&amp;&amp;&apos; (AND) operator to validate both operand, &apos;||&apos; (OR) operator check ny given expressions are true, &apos;!&apos; (NOT) check the result of (yr % 100 != 0). */ if ( (yr % 400 == 0) || (yr % 4 == 0 &amp;&amp; yr % 100 != 0)) { printf (&apos; %d is a leap year. 
&apos;, yr); } else { printf (&apos; %d is not a leap year. 
&apos;, yr); } return 0; } 

Çıktı:

 Enter the year: 2020 2020 is a leap year. 

Yukarıdaki programda 2020 yılına giriyoruz ve ardından if...else ifadesini tanımlayarak verilen yılı kontrol ediyoruz. Bu açıklamada iki koşulu tanımladık;

  1. Verilen yıl 400'e bölünür ve bu da 0'a eşittir. Daha sonra, sol veya sağ işlenen koşulunun doğru olup olmadığını kontrol etmek için mantıksal OR operatörünü kullanırız.
  2. İkinci durumda verilen yıl 4'e ve 100'e bölünüyor. Ama 2020'yi 4'e böldüğümüzde 0 oluyor. Benzer şekilde 2020 yılını da 0'a eşit olmayan 100'e bölüyoruz. '2020'nin artık yıl olduğunu' gösteren koşullar doğrudur.
  3. Ama 2021 yılına girdiğimizde '2021 artık yıl değil' sonucunu veriyor.

2veuygulamak:

java mysql ile bağlantı kurmak
 Enter the year: 2021 2021 is not a leap year. 

Örnek 4: AND, OR ve NOT mantıksal operatörünü kullanarak farklı koşulları kontrol eden program

C'deki AND, OR ve NOT mantıksal operatörünü kullanarak verilen işlenenlerin çoklu koşullarını gösterecek bir program yazalım.

 /* 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 &apos;AND&apos; and logical &apos;NOT&apos; operator if (a &gt; b &amp;&amp; a != 0) { printf (&apos; The AND (&amp;&amp;) operator said: Both conditions are true. 
 &apos;); } // use logical &apos;OR&apos; and logical &apos;NOT&apos; operator if (n1 &gt; n2 || n2 != 15) if ( ! (a &gt; b &amp;&amp; a != 0 )) { printf (&apos; The NOT (!) operator: Here both conditions are true. 
 &apos;); } else { printf (&apos; The NOT (!) operator: Here, both conditions are true. &apos; &apos; But, the status of the condition is reversed as false. 
&apos;); } return 0; } 

Çıktı:

 The AND (&amp;&amp;) 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.