Usage of && and || in c language

下次还敢
Release: 2024-04-28 09:43:07
Original
513 people have browsed it

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

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");
}
Copy after login

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");
}
Copy after login

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.

    If the first expression is false, the && operator will short-circuit evaluation, the second expression will not be evaluated, and the result will be false.
  • If the first expression is true, the || operator will short-circuit evaluation, the second expression will not be evaluated, and the result will be true.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!