| English | 释义 | 拼音 |
|---|---|---|
| equivalent/ɪˈkwɪvələnt/ | 等价 | děng jià |
| De Morgan's Laws/də ˈmɔːɡənz lɔːz/ | 德摩根定律 | dé mó gēn dìng lǜ |
One counterexample defeats a Boolean rewrite
- A proposed rewrite changes !(a && b) into !a && !b. Set a = true and b = false.
- The original is true, while the proposed rewrite is false. De Morgan requires !a || !b, which agrees with the original.

When two conditions mean the same
- Two Boolean expressions are equivalent 等价 if they give the same result for every input. For an integer
x,x >= 5and!(x < 5)are equivalent — both true exactly whenxis at least5. - For Boolean variables without side effects, equivalent expressions have the same result. Also check evaluation order if expressions call methods or change state. A truth table proves equivalence: same output in every row.
De Morgan's Laws
- De Morgan's Laws 德摩根定律 rewrite a NOT over an AND/OR:
!(a && b)equals!a || !b. !(a || b)equals!a && !b. The pattern: push the!inside, and flip&&↔||.
仅有一行相同不足以证明等价
对每组纯布尔输入比较原式、德摩根改写和未交换运算符的候选式;一次不匹配就是反例。
Simplifying conditions
- Use De Morgan's and logic rules to write a shorter, clearer equivalent. For an integer score,
!(score < 60)becomesscore >= 60— same meaning, easier to read. - Removing a double negative or an outer
!often clarifies the intent. Check the exact input domain and evaluation behavior; fewer characters do not guarantee fewer bugs.
由德摩根定律,!(a && b) 等价于……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
把 ! 推进去并把 && 翻转为 ||。
由德摩根定律,!(a || b) 等价于……
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
把 ! 推进去并把 || 翻转为 &&。
对于整数分数 score,它等价于 !(score < 60) 吗?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
“不小于 60”意思是“至少 60”。
如果两个布尔表达式对每个输入都给出相同结果,它们就等价。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
真值表每一行输出相同 = 等价。
应用德摩根定律,!(a && b) 变成 !a && !b。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
它变成 !a || !b——运算符必须翻转。
Testing with a truth table
- To check if two expressions are equivalent, tabulate both for all inputs. If every row matches, they're equivalent; one mismatch means they're not.
- For two pure Boolean variables there are four input rows, which fully determine the truth function. This does not enumerate the effects of arbitrary method calls or prove every rewrite of a floating-point comparison.

When applying De Morgan's Law, you must flip the operator too, not just distribute the !. !(a && b) is !a || !b (AND becomes OR) — writing !a && !b is wrong. The mnemonic: push the NOT in and swap && ↔ ||. When unsure, a truth table settles it in four rows.
当 a=true 且 b=false 时,按顺序求 !(a && b)、!a && !b 和 !a || !b 的值。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The mismatching middle expression is a counterexample to the proposed equivalence.
对于等于 NaN 的 双 类型分数 score,!(score < 60) and score >= 60) 返回相同的布尔结果。
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
NaN 的比较 score < 60 and score >= 60 均为假,因此第一个表达式的否定为真,第二个为假。
若 f() 返回 false,在计算 !(f() && g()) 的过程中 g() 被调用了几次?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
第一个操作数为假会短路 &&,因此不调用 g。等价的 !f() || !g() 也会跳过 g。
Rewriting !(a && b):
- Wrong:
!a && !b. - Right (De Morgan):
!a || !b— AND flips to OR. - Check: if
a = true, b = false, then!(true && false) = !false = true, and!a || !b = false || true = true. ✓
Carry the reasoning to a new case
- For pure Boolean variables, all four input pairs prove equivalence. De Morgan's forms preserve left-to-right short-circuit evaluation:
!(f() && g())and!f() || !g()both skip g when f is false. - Swapping operands is a different transformation. Although
a && bandb && ahave the same truth table for pure variables,f() && g()andg() && f()may call different methods. For double score=NaN,!(score < 60)is true butscore >= 60is false; the integer comparison rule does not extend to NaN.
Two Boolean expressions are equivalent if they agree for every input (provable with a truth table). De Morgan's Laws: !(a && b) = !a || !b and !(a || b) = !a && !b — push the ! inward and flip && ↔ ||. Use these to simplify conditions into clearer equivalent forms.