Comparando Expresiones Booleanas
Introducido| English | Significado |
|---|---|
| equivalent/ɪˈkwɪvələnt/ | equivalente |
| De Morgan's Laws/də ˈmɔːɡənz lɔːz/ | Leyes 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.
Por la Ley de De Morgan, !(a && b) es equivalente a...
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Introduce el ! y cambia && por ||.
Por la Ley de De Morgan, !(a || b) es equivalente a...
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Introduce el ! y cambia || por &&.
Para una puntuación entera, ¿cuál es equivalente a !(score < 60)?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
no-menos-que-60 significa al-menos-60.
Dos expresiones booleanas son equivalentes si producen el mismo resultado para cada entrada.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Mismo resultado en cada fila de la tabla de verdad = equivalente.
Aplicando la Ley de De Morgan, !(a && b) se convierte en !a && !b.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Se convierte en !a || !b — el operador debe cambiar.
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 y b=false, ¿cuáles son los valores de !(a && b), !a && !b y !a || !b, en ese orden?
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
The mismatching middle expression is a counterexample to the proposed equivalence.
Para una puntuación double igual a NaN, !(score < 60) and score >= 60 retornan el mismo resultado booleano.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
Las comparaciones score < 60 and score >= 60 son ambas falsas para NaN, por lo que la negación de la primera expresión es verdadera y la segunda es falsa.
Si f() devuelve false, ¿cuántas veces se ejecuta g() al evaluar !(f() && g())?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
El primer operando falso corta la evaluación de &&, por lo que g no se llama. El equivalente !f() || !g() también omite 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.