c语言中||和|运算符
Logical operators 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语言中的逻辑OR(||)运算符 (Logical OR (||) operator in C)
Logical OR is denoted by double pipe characters (||), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.
逻辑OR由双竖线字符( || )表示,用于检查多个条件的组合。 它是一个二进制运算符–需要两个操作数。
If any of the operand's values is non-zero (true), Logical OR (||) operator returns 1 ("true"), it returns 0 ("false") if all operand's values are 0 (false).
如果任何一个操作数的值都不为零 (true),则逻辑OR(||)运算符将返回1(“ true”),如果所有操作数的值均为0(false),它将返回0(“ false”)。
Syntax of Logical OR operator:
逻辑OR运算符的语法:
condition1 || condition2
Truth table of logical OR operator:
逻辑或运算符的真值表:
condition1 condition2 condition1 || condition2
1 1 1
1 0 1
0 1 1
0 0 0
逻辑OR(||)运算符的C示例 (C examples of Logical OR (||) operator )
Example 1: Take a number and apply some of the conditions and print the result of expression containing logical OR operator.
示例1:取一个数字并应用一些条件,并打印包含逻辑OR运算符的表达式的结果。
// C program to demonstrate example of
// Logical OR (||) operator
#include <stdio.h>
int main()
{
int num =10;
//printing result with OR (||) 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!=10));
return 0;
}
Output
输出量
1
1
1
0
Example 2: Input gender in single character and print full gender (Ex: if input is 'M' or 'm' – it should print "Male").
示例2:以单字符输入性别并打印完整性别(例如:如果输入为“ M”或“ m”,则应打印“ Male”)。
// Input gender in single character and print full gender
// (Ex: if input is 'M' or 'm' - it should print "Male").
#include <stdio.h>
int main()
{
char gender;
//input gender
printf("Enter gender (m/M/f/F): ");
scanf("%c", &gender);
//check the condition and print gender
if(gender=='m' || gender=='M')
printf("Gender is Male");
else if(gender=='f' || gender=='F')
printf("Gender is Female");
else
printf("Unspecified gender");
return 0;
}
Output
输出量
Enter gender (m/M/f/F): m
Gender is Male
Second run:
Enter gender (m/M/f/F): F
Gender is Female
Third run:
Enter gender (m/M/f/F): x
Unspecified gender
Example 3: Input an integer number and check whether it is divisible by 9 or 7.
示例3:输入一个整数并检查其是否可以被9或7整除。
// Input an integer number and check whether
// it is divisible by 9 or 7.
#include <stdio.h>
int main()
{
int num;
//input number
printf("Enter an integer number: ");
scanf("%d", &num);
//check the condition
if(num%9==0 || num%7==0)
printf("%d is divisible by 9 or 7\n",num);
else
printf("%d is not divisible by 9 or 7\n",num);
return 0;
}
Output
输出量
First run:
Enter an integer number: 27
27 is divisible by 9 or 7
Second run:
Enter an integer number: 49
49 is divisible by 9 or 7
Third run:
Enter an integer number: 25
25 is not divisible by 9 or 7
翻译自: https://www.includehelp.com/c/logical-or-operator-with-example-in-c.aspx
c语言中||和|运算符