logo

Operator logiczny AND w C

Operatory logiczne wykonują operacje logiczne na danym wyrażeniu, łącząc dwa lub więcej wyrażeń lub warunków. Można go używać w różnych wyrażeniach relacyjnych i warunkowych. Operator ten opiera się na wartościach logicznych w celu logicznego sprawdzenia warunku i jeśli warunki są prawdziwe, zwraca 1. W przeciwnym razie zwraca 0 (fałsz). W programowaniu w języku C operatory logiczne dzieli się na trzy typy, takie jak operator logiczny AND (&&), operator logiczny OR (||) i operator logiczny NOT (!). Tutaj dowiadujemy się o operatorze logicznym AND i jego różnych przykładach w języku programowania C.

Operator logiczny AND w C

Operator logiczny AND

Operator logiczny AND jest reprezentowany jako podwójny symbol ampersandu „&&”. Sprawdza warunek dwóch lub więcej operandów poprzez połączenie w wyrażeniu, a jeśli wszystkie warunki są prawdziwe, operator logiczny AND zwraca wartość logiczną true lub 1. W przeciwnym razie zwraca false lub 0.

Uwaga: Jeśli wartość obu jest różna od zera, warunek pozostanie spełniony. W przeciwnym razie operator logiczny AND (&&) zwraca 0 (fałsz).

Składnia

ciąg długości
 (condition1 && condition2) 

W powyższej składni występują dwa warunki, warunek 1 i warunek 2 oraz pomiędzy podwójnym symbolem ampersandu (&&). Jeśli oba warunki są spełnione, operator logiczny AND zwraca wartość logiczną 1 lub prawdę. W przeciwnym razie zwraca wartość false.

Tabela prawdy operatora logicznego AND (&&).

A B A i B
1 1 1
1 0 0
0 1 0
0 0 0

Przykład 1: Program demonstrujący operator logiczny AND w C

 #include #include int main () { // declare variable int n = 20; // use Logical AND (&&) operator to check the condition printf (' %d 
', (n == 20 && n >= 8)); // condition is true, therefore it returns 1 printf (' %d 
', (n >= 1 && n >= 20)); printf (' %d 
', (n == 10 && n >= 0)); printf (' %d 
&apos;, (n &gt;= 20 &amp;&amp; n <= 40)); return 0; } < pre> <p> <strong>Output</strong> </p> <pre> 1 1 0 1 </pre> <p> <strong>Example 2: Program to find the largest number using the Logical AND operator</strong> </p> <pre> #include #include int main () { // declare integer type variable int x, y, z; printf (&apos; Enter the first number: &apos;); scanf (&apos;%d&apos;, &amp;x); printf (&apos; Enter the second number: &apos;); scanf (&apos;%d&apos;, &amp;y); printf (&apos; Enter the third number: &apos;); scanf (&apos;%d&apos;, &amp;z); // use logical AND operator to validate the condition if ( x &gt;= y &amp;&amp; x &gt;= z ) { printf (&apos; %d is the largest number of all. &apos;, x); } else if ( y &gt;= x &amp;&amp; y &gt;= z) { printf (&apos; %d is the largest number of all. &apos;, y); } else { printf ( &apos; %d is the largest number of all. &apos;, z); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the first number: 20 Enter the second number: 10 Enter the third number: 50 50 is the largest number of all </pre> <p> <strong>Example 3: Program to use the Logical AND (&amp;&amp;) operator to check whether the user is teenager or not.</strong> </p> <pre> #include #include int main () { // declare variable int age; printf (&apos; Enter the age: &apos;); scanf (&apos; %d&apos;, &amp;age); // get age // use logical AND operator to check more than one condition if ( age &gt;= 13 &amp;&amp; age <= 19) { printf (' %d is a teenager age. ', age); } else not return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the age: 17 17 is a teenager age. 2nd execution: Enter the age: 10 10 is not a teenager age. </pre> <p> <strong>Example 4: Program to validate whether the entered number is in the defined range or not.</strong> </p> <pre> #include int main () { int num; printf (&apos; Enter a number between 1 to 50: &apos;); scanf (&apos; %d&apos;, &amp;num); //get the number // use logical AND operator to check condition if ( (num &gt; 0 ) &amp;&amp; (num 50 ) &amp;&amp; ( num <= 50 100)) { printf (' the entered number is in range and 100. '); } else please enter defined range. return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number between 1 to 50: 19 The entered number is in the range 0 and 50. 2nd Run Enter a number between 1 to 50: 51 The entered number is in the range 50 and 100. 3rd execution: Enter a number between 1 to 50: 0 Please enter the number is in the defined range. </pre> <p> <strong>Example 5: Program to validate the username and password entered by the user is correct or not using the predefined username and password.</strong> </p> <pre> #include #include // use #define macro to define the values for UserName and Password #define UserName &apos;system&apos; #define Password &apos;admin@123&apos; int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( &apos; Enter the username: &apos; ); scanf (&apos; %s&apos;, un); printf ( &apos; Enter the password: &apos; ); scanf (&apos; %s&apos;, pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 &amp;&amp; strcmp (Password, pass) == 0) { printf (&apos; 
 The user&apos;s credentials are correct. &apos;); } else { printf ( &apos; 
 The user&apos;s credentials are incorrect. &apos;); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user&apos;s credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user&apos;s credentials are incorrect. </pre> <hr></=></pre></=></pre></=>

Przykład 2: Program znajdujący największą liczbę za pomocą operatora logicznego AND

 #include #include int main () { // declare integer type variable int x, y, z; printf (&apos; Enter the first number: &apos;); scanf (&apos;%d&apos;, &amp;x); printf (&apos; Enter the second number: &apos;); scanf (&apos;%d&apos;, &amp;y); printf (&apos; Enter the third number: &apos;); scanf (&apos;%d&apos;, &amp;z); // use logical AND operator to validate the condition if ( x &gt;= y &amp;&amp; x &gt;= z ) { printf (&apos; %d is the largest number of all. &apos;, x); } else if ( y &gt;= x &amp;&amp; y &gt;= z) { printf (&apos; %d is the largest number of all. &apos;, y); } else { printf ( &apos; %d is the largest number of all. &apos;, z); } return 0; } 

Wyjście

 Enter the first number: 20 Enter the second number: 10 Enter the third number: 50 50 is the largest number of all 

Przykład 3: Program używający operatora logicznego AND (&&) do sprawdzania, czy użytkownik jest nastolatkiem, czy nie.

 #include #include int main () { // declare variable int age; printf (&apos; Enter the age: &apos;); scanf (&apos; %d&apos;, &amp;age); // get age // use logical AND operator to check more than one condition if ( age &gt;= 13 &amp;&amp; age <= 19) { printf (\' %d is a teenager age. \', age); } else not return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the age: 17 17 is a teenager age. 2nd execution: Enter the age: 10 10 is not a teenager age. </pre> <p> <strong>Example 4: Program to validate whether the entered number is in the defined range or not.</strong> </p> <pre> #include int main () { int num; printf (&apos; Enter a number between 1 to 50: &apos;); scanf (&apos; %d&apos;, &amp;num); //get the number // use logical AND operator to check condition if ( (num &gt; 0 ) &amp;&amp; (num 50 ) &amp;&amp; ( num <= 50 100)) { printf (\' the entered number is in range and 100. \'); } else please enter defined range. return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number between 1 to 50: 19 The entered number is in the range 0 and 50. 2nd Run Enter a number between 1 to 50: 51 The entered number is in the range 50 and 100. 3rd execution: Enter a number between 1 to 50: 0 Please enter the number is in the defined range. </pre> <p> <strong>Example 5: Program to validate the username and password entered by the user is correct or not using the predefined username and password.</strong> </p> <pre> #include #include // use #define macro to define the values for UserName and Password #define UserName &apos;system&apos; #define Password &apos;admin@123&apos; int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( &apos; Enter the username: &apos; ); scanf (&apos; %s&apos;, un); printf ( &apos; Enter the password: &apos; ); scanf (&apos; %s&apos;, pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 &amp;&amp; strcmp (Password, pass) == 0) { printf (&apos; 
 The user&apos;s credentials are correct. &apos;); } else { printf ( &apos; 
 The user&apos;s credentials are incorrect. &apos;); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user&apos;s credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user&apos;s credentials are incorrect. </pre> <hr></=></pre></=>

Przykład 4: Program sprawdzający, czy wprowadzona liczba mieści się w zdefiniowanym zakresie czy nie.

 #include int main () { int num; printf (&apos; Enter a number between 1 to 50: &apos;); scanf (&apos; %d&apos;, &amp;num); //get the number // use logical AND operator to check condition if ( (num &gt; 0 ) &amp;&amp; (num 50 ) &amp;&amp; ( num <= 50 100)) { printf (\' the entered number is in range and 100. \'); } else please enter defined range. return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a number between 1 to 50: 19 The entered number is in the range 0 and 50. 2nd Run Enter a number between 1 to 50: 51 The entered number is in the range 50 and 100. 3rd execution: Enter a number between 1 to 50: 0 Please enter the number is in the defined range. </pre> <p> <strong>Example 5: Program to validate the username and password entered by the user is correct or not using the predefined username and password.</strong> </p> <pre> #include #include // use #define macro to define the values for UserName and Password #define UserName &apos;system&apos; #define Password &apos;admin@123&apos; int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( &apos; Enter the username: &apos; ); scanf (&apos; %s&apos;, un); printf ( &apos; Enter the password: &apos; ); scanf (&apos; %s&apos;, pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 &amp;&amp; strcmp (Password, pass) == 0) { printf (&apos; 
 The user&apos;s credentials are correct. &apos;); } else { printf ( &apos; 
 The user&apos;s credentials are incorrect. &apos;); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user&apos;s credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user&apos;s credentials are incorrect. </pre> <hr></=>

Przykład 5: Program sprawdzający czy nazwa użytkownika i hasło wprowadzone przez użytkownika są prawidłowe lub nie korzystają z predefiniowanej nazwy użytkownika i hasła.

 #include #include // use #define macro to define the values for UserName and Password #define UserName &apos;system&apos; #define Password &apos;admin@123&apos; int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( &apos; Enter the username: &apos; ); scanf (&apos; %s&apos;, un); printf ( &apos; Enter the password: &apos; ); scanf (&apos; %s&apos;, pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 &amp;&amp; strcmp (Password, pass) == 0) { printf (&apos; 
 The user&apos;s credentials are correct. &apos;); } else { printf ( &apos; 
 The user&apos;s credentials are incorrect. &apos;); } return 0; } 

Wyjście

 Enter the username: system Enter the password: admin@123 The user&apos;s credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user&apos;s credentials are incorrect.