Logiske operatorer udfører logiske operationer på et givet udtryk ved at forbinde to eller flere udtryk eller betingelser. Det kan bruges i forskellige relationelle og betingede udtryk. Denne operator er baseret på booleske værdier for logisk at kontrollere betingelsen, og hvis betingelserne er sande, returnerer den 1. Ellers returnerer den 0 (False). I C-programmering klassificeres logiske operatorer i tre typer såsom den logiske AND (&&) operator, den logiske OR operator (||) og den logiske NOT (!) operator. Her lærer vi om den logiske OG-operator og dens forskellige eksempler i programmeringssproget C.
Logisk OG Operatør
Den logiske OG-operator er repræsenteret som det dobbelte og-tegn '&&'. Den kontrollerer betingelsen for to eller flere operander ved at kombinere i et udtryk, og hvis alle betingelserne er sande, returnerer den logiske OG-operator den boolske værdi sand eller 1. Ellers returnerer den falsk eller 0.
Bemærk: Hvis værdien af begge er ikke-nul, vil betingelsen forblive sand. Ellers returnerer den logiske AND (&&) operator 0 (falsk).
Syntaks
global var i js
(condition1 && condition2)
Der er to betingelser i ovenstående syntaks, betingelse1 og betingelse2, og derimellem det dobbelte (&&) og-tegn. Hvis begge betingelser er sande, returnerer den logiske OG-operator boolsk værdi 1 eller sand. Ellers returnerer den falsk.
Sandhedstabel for den logiske AND (&&) operator
EN | B | A && B |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Eksempel 1: Program til at demonstrere den logiske OG-operatør i 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></=>
Eksempel 2: Program til at finde det største tal ved hjælp af logisk OG-operatoren
funktioner i en panda-serie
#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; }
Produktion
Enter the first number: 20 Enter the second number: 10 Enter the third number: 50 50 is the largest number of all
Eksempel 3: Program til at bruge operatoren Logical AND (&&) til at kontrollere, om brugeren er teenager eller ej.
xor cpp
#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></=>
Eksempel 4: Program til at validere, om det indtastede tal er inden for det definerede område eller ej.
#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></=>
Eksempel 5: Program til at validere, at brugernavnet og adgangskoden, som brugeren har indtastet, er korrekt eller ikke bruger det foruddefinerede brugernavn og kodeord.
#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; }
Produktion
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.
=>=>=>