Compound Boolean Expressions · 复合布尔表达式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| AND/ænd/ | 逻辑与 | luó jí yǔ |
| OR/ɔː/ | 逻辑或 | luó jí huò |
| NOT/nɒt/ | 逻辑非 | luó jí fēi |
| short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ | 短路求值 | duǎn lù qiú zhí |
AND and OR
- Combine conditions with
&&(logical AND 逻辑与) and||(logical OR 逻辑或). a && bistrueonly when both aretrue.a || bistruewhen at least one istrue.- "Is the number between 1 and 10?" →
x >= 1 && x <= 10.
与和或
- 用
&&(逻辑与)和||(逻辑或)组合条件。 a && b只在两者都为true时才为true。a || b在至少一个为true时为true。- “这个数在 1 到 10 之间吗?” →
x >= 1 && x <= 10。
NOT reverses
!(logical NOT 逻辑非) flips a Boolean:!trueisfalse,!falseistrue.!(x > 5)istrueexactly whenxis not greater than5.- NOT applies to the expression right after it — use parentheses for clarity.
- Double negation cancels:
!!aequalsa.
非会反转
!(逻辑非)翻转一个布尔值:!true是false,!false是true。!(x > 5)恰好在x不大于5时为true。- 非应用于紧跟它的表达式——用括号以求清晰。
- 双重否定抵消:
!!a等于a。
Short-circuit evaluation
&&and||stop as soon as the answer is known — this is short-circuit evaluation 短路求值.- For
a && b: ifaisfalse, the result isfalseandbis never evaluated. - For
a || b: ifaistrue, the result istrueandbis skipped. - This lets you guard:
x != 0 && 10 / x > 2avoids dividing by zero.
短路求值
&&和||一旦答案已知就停止——这就是短路求值。- 对
a && b:如果a为false,结果就是false,b从不被求值。 - 对
a || b:如果a为true,结果就是true,b被跳过。 - 这让你能设防:
x != 0 && 10 / x > 2避免了除以零。
Truth tables and parentheses
- A truth table lists every combination of inputs and the expression's result.
- With two variables there are $4$ rows; build one to check tricky logic.
- Use parentheses to make precedence explicit:
(a && b) || c. - Without them,
&&binds tighter than||— a common source of confusion.
真值表与括号
- 真值表列出每一种输入组合以及表达式的结果。
- 两个变量有 $4$ 行;对棘手的逻辑建一张来检查。
- 用括号让优先级显式:
(a && b) || c。 - 没有它们,
&&比||结合更紧——一个常见的困惑源。
Short-circuiting isn't just an optimization — it can prevent errors. In x != 0 && 10 / x > 2, if x is 0 the left side is false, so Java never evaluates 10 / x and avoids a divide-by-zero. Swap the order and it crashes. Also, && binds tighter than ||, so parenthesize mixed expressions to say exactly what you mean.
**短路不只是一种优化——它能防止错误。**在 x != 0 && 10 / x > 2 中,如果 x 为 0,左侧为 false,所以 Java 从不求 10 / x,避免了除以零。把顺序对调它就崩溃。此外,&& 比 || 结合更紧,所以给混合表达式加括号,以准确表达你的意思。
Is x outside 1–10?
x < 1 || x > 10→truewhenxis too small or too big.x = 15→false || true→true(outside).x = 5→false || false→false(inside).
x 是否在 1–10 之外?
x < 1 || x > 10→ 当x太小或太大时为true。x = 15→false || true→true(在外面)。x = 5→false || false→false(在里面)。
Combine conditions with && (both true), || (at least one true), and ! (reverse). Java uses short-circuit evaluation: &&/|| stop once the result is known (handy for guards like x != 0 && 10/x > 2). Build a truth table for tricky logic and use parentheses — && binds tighter than ||.
用 &&(都为真)、||(至少一个为真)和 !(反转)组合条件。Java 使用短路求值:&&/|| 一旦结果已知就停(对 x != 0 && 10/x > 2 这样的设防很有用)。对棘手逻辑建真值表并用括号——&& 比 || 结合更紧。
AND of two conditions · 两个条件的与
&& is true only when both inputs are true. · && 只在两个输入都为真时为真。
a && b is true when... · a && b 在……时为真。
&& (AND) needs both operands true. · &&(与)需要两个操作数都为真。
In a && b, if a is false, then b is... · 在 a && b 中,如果 a 为假,那么 b……
&& short-circuits: a false left side makes the result false immediately. · && 短路:左侧为假立即让结果为假。
!(true) evaluates to false. · !(true) 求值为 false。
! reverses the Boolean value. · ! 反转布尔值。
With x = 5, what does x < 1 || x > 10 evaluate to? · 当 x = 5 时,x < 1 || x > 10 求值为什么?
false || false → false (5 is inside 1..10). · false || false → false(5 在 1..10 内)。
The behavior where && and || stop as soon as the result is known is called ___-circuit evaluation (one word). · && 和 || 一旦结果已知就停止的行为叫做 ___ 求值(填英文一词 short)。
Short-circuit evaluation stops early. · 短路求值提前停止。