logo

عامل التشغيل المنطقي NOT (!) في C

سيناقش هذا القسم عامل التشغيل NOT (!) المنطقي في لغة البرمجة C. كما نعلم بالفعل، يتم استخدام العامل المنطقي لإجراء العملية المنطقية من خلال الجمع بين شرطين أو أكثر في التعبيرات المحددة. إذا كانت الشروط المنطقية للمعاملات صحيحة، يقوم عامل التشغيل بإرجاع قيم منطقية صحيحة أو 1. وبخلاف ذلك، فإنه يُرجع قيمة منطقية خاطئة أو 0. يتم تصنيف العوامل المنطقية إلى ثلاثة أجزاء: العوامل المنطقية AND، والمنطقية OR، والمنطقية NOT.

عامل التشغيل المنطقي NOT (!) في C

ال المنطقي والمشغل يتم استخدامه للتحقق من شروط اثنين أو أكثر من المعاملات التي تظل صحيحة في تعبير معين؛ يقوم عامل التشغيل AND بإرجاع قيمة صحيحة أو غير صفرية (1). وإلا، فإنها ترجع قيمة خاطئة أو 0. لذا، يمكننا القول أن عامل التشغيل AND المنطقي لا يمكنه تنفيذ إلا في تعبير عندما تكون شروط كلا المعاملين صحيحة، وإذا كان أي شرط غير صحيح، فإنه يُرجع 0. يتم تمثيل عامل التشغيل المنطقي AND كرمز علامة الضم المزدوجة '&&'.

بناء الجملة:

 (A > b && b > c) 

ال عامل منطقي أو يتم استخدامه للتحقق من شروط كلا المعاملين (A & B)، وإذا كان أحد المعاملين أو التعبيرات صحيحًا، يقوم عامل التشغيل بإرجاع قيمة منطقية حقيقية. وبالمثل، إذا لم يكن أي من التعبيرات صحيحًا، فسيتم إرجاع قيمة خاطئة أو صفر. تتم الإشارة إلى عامل التشغيل المنطقي OR باعتباره الأنبوب المزدوج '||' رمز.

بناء الجملة:

 (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)>

هنا، '!' يمثل الرمز عامل التشغيل NOT المنطقي، والذي يعكس نتيجة الشرط المحدد.

جدول الحقيقة للعامل NOT المنطقي:

فيما يلي جدول الحقيقة للعامل المنطقي غير في لغة C

 condition !(condition) 1 0 0 1 

مثال 1: برنامج لاستخدام عامل التشغيل NOT المنطقي في لغة C

لنقم بإنشاء برنامج بسيط لعكس الشرط المحدد للمعاملات في لغة البرمجة C.

 /* 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));>

في البرنامج أعلاه، نستخدم عامل التشغيل NOT (!) لعكس نتيجة التعبير المتنوع، مثل شرط المتغير x يساوي 5، وهو صحيح. ومع ذلك، فإن عامل التشغيل NOT يعكس النتيجة ويعيد 0. وبالمثل، قمنا بتحديد الشرط (x!=5)، لكن العامل المنطقي غيّر نتيجته وأعاد 1 وهكذا.

مثال 2: برنامج لإدخال رقم لإجراء عامل التشغيل NOT المنطقي

لنقم بإنشاء برنامج بسيط للحصول على النتيجة العكسية لعدد صحيح باستخدام العامل المنطقي NOT (!) في لغة البرمجة C.

 /* 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; } 

انتاج:

 Enter the number: 7 The result of x: 0 

في البرنامج أعلاه، نقوم بإدخال عدد صحيح رقم 7 من المستخدم وتخزينه في المتغير x. بعد ذلك، يقوم عامل التشغيل المنطقي NOT (!) بعكس قيمة x (غير الصفر) ويعيد الصفر (0) لطباعة نتيجة x.

2اختصار الثانيتنفيذ:

 Enter the number: 0 The result of x: 1 

وبالمثل، نقوم بإدخال صفر (0) من المستخدم واستخدام عامل التشغيل المنطقي NOT (!) لعكس قيمة x إلى قيمة غير الصفر، وهي 1.

مثال 3: برنامج للعثور على السنة الكبيسة باستخدام عامل التشغيل المنطقي AND (&&) وOR (||) وليس (!)

دعونا نكتب برنامجًا بسيطًا للتحقق مما إذا كانت السنة المحددة كبيسة أم لا باستخدام عامل التشغيل المنطقي AND (&&)، والعامل المنطقي OR (||)، والعامل المنطقي NOT (!) في لغة C.

 #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; } 

انتاج:

 Enter the year: 2020 2020 is a leap year. 

في البرنامج أعلاه، ندخل عام 2020 ثم نتحقق من السنة المحددة من خلال تحديد عبارة if...else. وقد حددنا في هذا البيان شرطين؛

  1. يتم تقسيم السنة المحددة على 400، وهو ما يساوي 0. وبعد ذلك، نستخدم عامل التشغيل المنطقي OR للتحقق مما إذا كان شرط المعامل الأيسر أو الأيمن صحيحًا.
  2. وفي الشرط الثاني، يتم قسمة السنة المحددة على 4 و100. لكن عندما نقسم 2020 على 4، وهو ما يساوي 0. وبالمثل، نقسم سنة 2020 على 100، وهو أيضًا لا يساوي 0. إذن، كلاهما الشروط صحيحة لعرض '2020 سنة كبيسة'.
  3. ولكن عندما ندخل عام 2021، فإنه يطبع النتيجة المعطاة '2021 ليست سنة كبيسة'.

2اختصار الثانيتنفيذ:

 Enter the year: 2021 2021 is not a leap year. 

مثال 4: برنامج للتحقق من الشروط المختلفة باستخدام عامل التشغيل AND وOR وNOT

دعونا نكتب برنامجًا لتوضيح الشروط المتعددة للمعاملات المحددة باستخدام العامل AND وOR والعامل المنطقي NOT في لغة C.

جافا شجرة ثنائية
 /* 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; } 

انتاج:

 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.