Els operadors lògics realitzen operacions lògiques sobre una expressió determinada unint dues o més expressions o condicions. Es pot utilitzar en diverses expressions relacionals i condicionals. Aquest operador es basa en valors booleans per comprovar lògicament la condició, i si les condicions són certes, retorna 1. En cas contrari, retorna 0 (fals). A la programació en C, els operadors lògics es classifiquen en tres tipus, com ara l'operador AND lògic (&&), l'operador OR lògic (||) i l'operador NOT lògic (!). Aquí coneixem l'operador AND lògic i els seus diversos exemples en el llenguatge de programació C.
Operador I lògic
L'operador AND lògic es representa com el símbol doble '&&'. Comprova la condició de dos o més operands combinant-los en una expressió i, si totes les condicions són certes, l'operador AND lògic retorna el valor booleà cert o 1. En cas contrari, retorna fals o 0.
Nota: si el valor de tots dos és diferent de zero, la condició continuarà sent certa. En cas contrari, l'operador lògic AND (&&) retorna 0 (fals).
Sintaxi
(condition1 && condition2)
Hi ha dues condicions a la sintaxi anterior, condició1 i condició2, i entremig el símbol doble (&&). Si les dues condicions són certes, l'operador AND lògic retorna el valor booleà 1 o cert. En cas contrari, retorna false.
Taula de veritat de l'operador AND lògic (&&).
A | B | A & B |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Exemple 1: programa per demostrar l'operador AND lògic a 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 ', (n >= 20 && 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 (' Enter the first number: '); scanf ('%d', &x); printf (' Enter the second number: '); scanf ('%d', &y); printf (' Enter the third number: '); scanf ('%d', &z); // use logical AND operator to validate the condition if ( x >= y && x >= z ) { printf (' %d is the largest number of all. ', x); } else if ( y >= x && y >= z) { printf (' %d is the largest number of all. ', y); } else { printf ( ' %d is the largest number of all. ', 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 (&&) operator to check whether the user is teenager or not.</strong> </p> <pre> #include #include int main () { // declare variable int age; printf (' Enter the age: '); scanf (' %d', &age); // get age // use logical AND operator to check more than one condition if ( age >= 13 && 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 (' Enter a number between 1 to 50: '); scanf (' %d', &num); //get the number // use logical AND operator to check condition if ( (num > 0 ) && (num 50 ) && ( 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 'system' #define Password 'admin@123' int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( ' Enter the username: ' ); scanf (' %s', un); printf ( ' Enter the password: ' ); scanf (' %s', pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 && strcmp (Password, pass) == 0) { printf (' The user's credentials are correct. '); } else { printf ( ' The user's credentials are incorrect. '); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user's credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user's credentials are incorrect. </pre> <hr></=></pre></=></pre></=>
Exemple 2: Programa per trobar el nombre més gran utilitzant l'operador AND lògic
#include #include int main () { // declare integer type variable int x, y, z; printf (' Enter the first number: '); scanf ('%d', &x); printf (' Enter the second number: '); scanf ('%d', &y); printf (' Enter the third number: '); scanf ('%d', &z); // use logical AND operator to validate the condition if ( x >= y && x >= z ) { printf (' %d is the largest number of all. ', x); } else if ( y >= x && y >= z) { printf (' %d is the largest number of all. ', y); } else { printf ( ' %d is the largest number of all. ', z); } return 0; }
Sortida
Enter the first number: 20 Enter the second number: 10 Enter the third number: 50 50 is the largest number of all
Exemple 3: Programa per utilitzar l'operador AND lògic (&&) per comprovar si l'usuari és adolescent o no.
#include #include int main () { // declare variable int age; printf (' Enter the age: '); scanf (' %d', &age); // get age // use logical AND operator to check more than one condition if ( age >= 13 && 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 (' Enter a number between 1 to 50: '); scanf (' %d', &num); //get the number // use logical AND operator to check condition if ( (num > 0 ) && (num 50 ) && ( 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 'system' #define Password 'admin@123' int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( ' Enter the username: ' ); scanf (' %s', un); printf ( ' Enter the password: ' ); scanf (' %s', pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 && strcmp (Password, pass) == 0) { printf (' The user's credentials are correct. '); } else { printf ( ' The user's credentials are incorrect. '); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user's credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user's credentials are incorrect. </pre> <hr></=></pre></=>
Exemple 4: Programa per validar si el número introduït es troba dins de l'interval definit o no.
#include int main () { int num; printf (' Enter a number between 1 to 50: '); scanf (' %d', &num); //get the number // use logical AND operator to check condition if ( (num > 0 ) && (num 50 ) && ( 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 'system' #define Password 'admin@123' int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( ' Enter the username: ' ); scanf (' %s', un); printf ( ' Enter the password: ' ); scanf (' %s', pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 && strcmp (Password, pass) == 0) { printf (' The user's credentials are correct. '); } else { printf ( ' The user's credentials are incorrect. '); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the username: system Enter the password: admin@123 The user's credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user's credentials are incorrect. </pre> <hr></=>
Exemple 5: El programa per validar el nom d'usuari i la contrasenya introduïts per l'usuari és correcte o no utilitza el nom d'usuari i la contrasenya predefinits.
#include #include // use #define macro to define the values for UserName and Password #define UserName 'system' #define Password 'admin@123' int main () { // declare character type array char un[50], pass[50]; // take UserName and Password from user printf ( ' Enter the username: ' ); scanf (' %s', un); printf ( ' Enter the password: ' ); scanf (' %s', pass); // use if statement and Logical AND operator to validate the condition if (strcmp (UserName, un) == 0 && strcmp (Password, pass) == 0) { printf (' The user's credentials are correct. '); } else { printf ( ' The user's credentials are incorrect. '); } return 0; }
Sortida
Enter the username: system Enter the password: admin@123 The user's credentials are correct. 2nd execution Enter the username: system Enter the password: admin@1234 The user's credentials are incorrect.
=>=>=>