Home > Article > Backend Development > Usage of && and || in c language
The && (logical AND) and || (logical OR) operators in C language are used to combine conditional expressions. The && operator determines that both expressions are true; the || operator determines that at least one of the two expressions is true. The operator has high precedence and supports short-circuit evaluation. It is executed first in the expression. If the first expression is sufficient to determine the result, the second expression is not evaluated.
Usage of && and || in C language
Introduction
In In C language, && (logical AND) and || (logical OR) are commonly used logical operators in conditional expressions. They are used to combine multiple conditions and determine the final result of an expression.
Logical AND (&&)
&& operator is used to determine whether two Boolean expressions are both true. If both expressions are true, the result is true. Otherwise, the result is false.
Syntax: expr1 && expr2
Example:
int age = 25; int salary = 50000; if (age >= 18 && salary >= 30000) { // 满足两个条件 printf("符合条件\n"); } else { // 不满足两个条件 printf("不符合条件\n"); }
Logical OR (||)
|| operator is used to determine whether at least one of two Boolean expressions is true. If both expressions are true, or one of them is true, the result is true. Otherwise, the result is false.
Syntax: expr1 || expr2
Example:
int age = 17; int hasExperience = 1; if (age >= 18 || hasExperience) { // 满足其中一个条件 printf("符合条件\n"); } else { // 不满足任何条件 printf("不符合条件\n"); }
Operation priority## The #&& and || operators have high precedence, second only to unary operators (such as !). Therefore, they are executed before most other operators.
Short-circuit evaluationThe&& and || operators also support short-circuit evaluation. This means that if the result of the first expression is sufficient to determine the final result, the second expression is not evaluated.
The above is the detailed content of Usage of && and || in c language. For more information, please follow other related articles on the PHP Chinese website!