Comparando Expressões Booleanas
Introduzido| English | Significado |
|---|---|
| equivalent/ɪˈkwɪvələnt/ | equivalente |
| De Morgan's Laws/də ˈmɔːɡənz lɔːz/ | Leis de De Morgan |
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&&↔||.
One matching row is not enough
Compare the original, De Morgan rewrite and unflipped candidate for every pair of pure Boolean inputs. A mismatch is a counterexample.
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.
Pela Lei de De Morgan, !(a && b) é equivalente a...
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Empurre o ! para dentro e inverta && para ||.
Pela Lei de De Morgan, !(a || b) é equivalente a...
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Empurre o ! para dentro e inverta || para &&.
Para uma pontuação inteira, qual é a equivalente de !(score < 60)?
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
não-menor-que-60 significa pelo-menos-60.
Duas expressões booleanas são equivalentes se produzirem o mesmo resultado para toda entrada.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Mesma saída em cada linha da tabela-verdade = equivalente.
Aplicando a Lei de De Morgan, !(a && b) torna-se !a && !b.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Torna-se !a || !b — o operador deve inverter.
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.
Para a=true e b=false, quais são os valores de !(a && b), !a && !b e !a || !b, nessa ordem?
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
The mismatching middle expression is a counterexample to the proposed equivalence.
Para uma pontuação dupla igual a NaN, !(score < 60) and score >= 60) retorna o mesmo resultado Booleano.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
As comparações score < 60 and score >= 60 são ambas falsas para NaN, portanto a negação da primeira expressão é verdadeira e a segunda é falsa.
Se f() retornar falso, quantas vezes g() será executada ao avaliar !(f() && g())?
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
O primeiro operando falso causa curto-circuito no &&, então g não é chamado. A equivalente !f() || !g() também pula 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.