Home  >  Article  >  How to use && and || in C language

How to use && and || in C language

小老鼠
小老鼠Original
2024-03-25 15:49:46842browse

In C language, && (logical AND) and || (logical OR) are commonly used logical operators, used to combine multiple conditional expressions. && requires all conditions to be true to be true, while || is true as long as one condition is true. These operators have lower precedence than relational operators but higher precedence than assignment operators. In control structures, they are used to execute different blocks of code based on conditions, such as if, while, for, etc.

How to use && and || in C language

In C language, && (logical AND) and || (logical OR) are two commonly used logical operators for combination Multiple conditional expressions to control the flow of a program based on whether those conditions are true or false.

1, && (logical AND)

&& operator is used to connect two or more conditional expressions, requiring all conditions to be is true (non-zero or non-null), the result of the entire expression is true. If any one of these conditions is false (zero or empty), the entire expression evaluates to false.

Example:

c

##

#include <stdio.h>  
  
int main() {  
    int a = 5;  
    int b = 10;  
  
    if (a > 0 && b > 0) {  
        printf("a 和 b 都大于 0\n"); // 这个会打印,因为 a 和 b 都大于 0  
    }  
  
    if (a > 10 && b > 0) {  
        printf("a 大于 10 且 b 大于 0\n"); // 这个不会打印,因为 a 不大于 10  
    }  
  
    return 0;  
}

2, || (logical OR)

|| operator is used to connect two or more conditional expressions, as long as at least one of the conditions is true (non-zero or not empty), the entire expression evaluates to true. The entire expression evaluates to false only if all conditions are false.

Example:

c

##

#include <stdio.h>  
  
int main() {  
    int a = 5;  
    int b = 0;  
  
    if (a > 0 || b > 0) {  
        printf("a 或 b 至少有一个大于 0\n"); // 这个会打印,因为 a 大于 0  
    }  
  
    if (a < 0 || b < 0) {  
        printf("a 或 b 至少有一个小于 0\n"); // 这个不会打印,因为 a 和 b 都不小于 0  
    }  
  
    return 0;  
}

Note

##When using the && or || operator, if the condition on the left can already determine the result of the entire expression, the condition on the right will not be evaluated. This is called For "short-circuit evaluation". For example, in if (a bcb8e456937106a85aa2d92c0bbd1918, 95ec6993dc754240360e28e0de8de30a=, <=), but higher than assignment operator (such as =). Therefore, in complex expressions, you may need to use parentheses to explicitly specify the order of operations.

&& and || operators are often used with control structures such as if, while, for, etc., to execute different blocks of code depending on whether a condition is true or false.

The above is the detailed content of How to use && and || in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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