“布尔运算符”的版本间的差异
(以“These can be used inside the condition of an if statement.”为内容创建页面) |
|||
第1行: | 第1行: | ||
These can be used inside the condition of an [[If(条件判断语句) 和 比较运算符 | if]] statement. | These can be used inside the condition of an [[If(条件判断语句) 和 比较运算符 | if]] statement. | ||
+ | |||
+ | 这些可以使用于[[If(条件判断语句) 和 比较运算符 | if]] 语句中的条件判断。 | ||
+ | |||
+ | |||
+ | |||
+ | <font color="orange" size="+1">'''&& (逻辑与)'''</font> | ||
+ | |||
+ | |||
+ | True only if both operands are true, e.g. | ||
+ | |||
+ | <pre style="color:dimgray"> | ||
+ | if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // read two switches | ||
+ | // ... | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | is true only if both inputs are high. | ||
+ | |||
+ | |||
+ | |||
+ | <font color="orange" size="+1">'''|| (逻辑或)'''</font> | ||
+ | |||
+ | |||
+ | True if either operand is true, e.g. | ||
+ | |||
+ | <pre style="color:dimgray"> | ||
+ | if (x > 0 || y > 0) { | ||
+ | // ... | ||
+ | } | ||
+ | </pre> | ||
+ | is true if either x or y is greater than 0. | ||
+ | |||
+ | |||
+ | |||
+ | <font color="orange" size="+1">'''! (取反)'''</font> | ||
+ | |||
+ | |||
+ | True if the operand is false, e.g. | ||
+ | |||
+ | <pre style="color:dimgray"> | ||
+ | if (!x) { | ||
+ | // ... | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | is true if x is false (i.e. if x equals 0). | ||
+ | |||
+ | |||
+ | <font color="darkorenge">'''警告:'''</font><br/> | ||
+ | |||
+ | |||
+ | Make sure you don't mistake the boolean AND operator, && (double ampersand) for the bitwise AND operator & (single ampersand). They are entirely different beasts. | ||
+ | |||
+ | |||
+ | Similarly, do not confuse the boolean || (double pipe) operator with the bitwise OR operator | (single pipe). | ||
+ | |||
+ | |||
+ | The bitwise not ~ (tilde) looks much different than the boolean not ! (exclamation point or "bang" as the programmers say) but you still have to be sure which one you want where. | ||
+ | |||
+ | |||
+ | |||
+ | <font color="orange" size="+1">'''示例'''</font> | ||
+ | |||
+ | <pre style="color:dimgray"> | ||
+ | if (a >= 10 && a <= 20){} // true if a is between 10 and 20 | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | [[Arduino语法参考 | 返回Arduino语法参考列表]] | ||
+ | |||
+ | 更多建议和问题欢迎反馈至 [http://www.yfrobot.com YFRobot论坛] |
2015年8月19日 (三) 14:36的最后版本
These can be used inside the condition of an if statement.
这些可以使用于 if 语句中的条件判断。
&& (逻辑与)
True only if both operands are true, e.g.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // read two switches // ... }
is true only if both inputs are high.
|| (逻辑或)
True if either operand is true, e.g.
if (x > 0 || y > 0) { // ... }
is true if either x or y is greater than 0.
! (取反)
True if the operand is false, e.g.
if (!x) { // ... }
is true if x is false (i.e. if x equals 0).
警告:
Make sure you don't mistake the boolean AND operator, && (double ampersand) for the bitwise AND operator & (single ampersand). They are entirely different beasts.
Similarly, do not confuse the boolean || (double pipe) operator with the bitwise OR operator | (single pipe).
The bitwise not ~ (tilde) looks much different than the boolean not ! (exclamation point or "bang" as the programmers say) but you still have to be sure which one you want where.
示例
if (a >= 10 && a <= 20){} // true if a is between 10 and 20
更多建议和问题欢迎反馈至 YFRobot论坛