Home > Article > Backend Development > The difference between || and && in C language
In C language, the || operator is true if at least one of its operands is true, while the && operator is true if all of its operands are true. || ignores subsequent true operands, while && stops evaluation when a false operand is found. Their precedence is higher than comparison operators but lower than assignment operators.
The difference between || and && in C language
In C language, && and || are logical Operator used to combine two or more Boolean expressions.
|| (Logical OR)
|| The operator checks whether at least one of its operands is true:
&(logical AND)
&& operator checks whether its operands are both true:
Difference
|| and && is their behavior with the False operand:
Example
int a = 1, b = 0, c = 1; printf("a || b || c: %d\n", a || b || c); // 输出:1 printf("a & b & c: %d\n", a & b & c); // 输出:0
In the first example, because a is true, the || operator ignores the values of b and c.
In the second example, because b is false, the && operator stops evaluating and returns False even though c is true.
Priority
|| and && have higher precedence than comparison operators (==, !=, >, <, >=, < ;=), but lower than the assignment operator (=).
The above is the detailed content of The difference between || and && in C language. For more information, please follow other related articles on the PHP Chinese website!