work with the test conditions and return the result based on the condition's results, these can also be used to validate multiple conditions together.
邏輯運算符使用測試條件并根據條件的結果傳回結果,這些
邏輯運算符還可用于一起驗證多個條件。
In C programming language, there are three logical operators
Logical AND (&&),
Logical OR (||)and
Logician NOT (!).
在C程式設計語言中,有三個邏輯運算符
Logical AND(&&),
Logical OR(||)和
Logician NOT(!)。
C語言中的邏輯AND(&&)運算符 (Logical AND (&&) operator in C)
Logical ANDis denoted by double ampersand characters (&&), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.
邏輯與通過雙符号字元(&&),則用于檢查的一個以上的條件的組合表示; 它是一個二進制運算符–需要兩個操作數。
If both of the operand's values is
non-zero(true),
Logical AND (&&) operatorreturns 1 (true), else it returns 0 (false).
如果兩個操作數的值都不
為零(true),則
邏輯AND(&&)運算符傳回1(true),否則傳回0(false)。
Syntax of Logical AND operator: 邏輯AND運算符的文法:condition1 && condition2
Truth table of logical AND operator: 邏輯AND運算符的真值表: condition1 condition2 condition1 && condition2
1 1 1
1 0 0
0 1 0
0 0 0
邏輯AND(&&)運算符的C示例 (C examples of Logical AND (&&) operator )
Example 1:Take a number and apply some of the conditions and print the result of expression containing logical AND operator.
示例1:取一個數字并應用一些條件,并列印包含邏輯AND運算符的表達式的結果。
// C program to demonstrate example of
// Logical AND (&&) operator
#include <stdio.h>
int main()
{
int num =10;
//printing result with AND (&&) operator
printf("%d\n",(num==10 && num>=5));
printf("%d\n",(num>=5 && num<=50));
printf("%d\n",(num!=10 && num>=5));
printf("%d\n",(num>=20 && num<=50));
return 0;
}
Output 輸出量 1
1
0
0
Example 2: Input age and check it is teenage or not.
示例2:輸入年齡并檢查年齡是否未滿。
// C program to demonstrate example of
// Logical AND (&&) operator
// Input age and check it is teenage or not.
#include <stdio.h>
int main()
{
int age;
//input age
printf("Enter age: ");
scanf("%d", &age);
//check teenage or not
if(age>=13 && age<=19)
printf("%d is a teenage value\n", age);
else
printf("%d is not a teenage value\n", age);
return 0;
}
Output 輸出量 First run:
Enter age: 18
18 is a teenage value
Second run:
Enter age: 12
12 is not a teenage value
Example 3: Input username and password and validate the values with predefined values of username and password.
示例3:輸入使用者名和密碼,并使用使用者名和密碼的預定義值驗證值。
// C program to demonstrate example of
// Logical AND (&&) operator
// Input username and password and validate the values
// with predefined values of username and password.
#include <stdio.h>
#include <string.h>
// define a fixed value for USERNAME and PASSWORD
#define USERNAME "admin"
#define PASSWORD "[email protected]"
int main()
{
char un[50], pass[50];
//input USERNAME and PASSWORD
printf("Enter username: ");
scanf("%s", un);
printf("Enter password: ");
scanf("%s", pass);
//validate username and password
if(strcmp(USERNAME,un)==0 && strcmp(PASSWORD, pass)==0)
printf("Input credentials are correct\n");
else
printf("Input credentials are not correct\n");
return 0;
}
Output 輸出量 First run:
Enter username: admin
Enter password: [email protected]
Input credentials are correct
Second run:
Enter username: admin
Enter password: [email protected]
Input credentials are not correct
翻譯自: https://www.includehelp.com/c/logical-and-operator-with-example-in-c.aspx