Compound Boolean Expressions · Expresiones Booleanas Compuestas
| English | Español |
|---|---|
| AND/ænd/ | AND |
| OR/ɔː/ | OR |
| NOT/nɒt/ | NOT |
| short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ | evaluación de cortocircuito |
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.
AND y OR
- Combine condiciones con
&&(AND lógico 逻辑与) y||(OR lógico 逻辑或). a && bestruesolo cuando ambos sontrue.a || bestruecuando al menos uno estrue.- "¿Está el número entre 1 y 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.
NOT invierte
!(NOT lógico 逻辑非) invierte un valor Booleano:!trueesfalse,!falseestrue.!(x > 5)estrueexactamente cuandoxno es mayor que5.- NOT se aplica a la expresión que le sigue; use paréntesis para mayor claridad.
- La doble negación se cancela:
!!aes igual aa.
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.
Evaluación de cortocircuito
&&y||se detienen tan pronto como se conoce el resultado — esto se conoce como evaluación de cortocircuito 短路求值.- Para
a && b: siaesfalse, el resultado esfalseybnunca se evalúa. - Para
a || b: siaestrue, el resultado estrueybse omite. - Esto permite proteger el código:
x != 0 && 10 / x > 2evita la división por cero.
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.
Tablas de verdad y paréntesis
- Una tabla de verdad enumera todas las combinaciones de entradas y el resultado de la expresión.
- Con dos variables hay $4$ filas; construya una para verificar lógicas complejas.
- Use paréntesis para hacer explícita la precedencia:
(a && b) || c. - Sin ellos,
&&tiene mayor precedencia que||— una fuente común de confusión.
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.
El cortocircuito no es solo una optimización; puede prevenir errores. En x != 0 && 10 / x > 2, si x es 0, el lado izquierdo es false, por lo que Java nunca evalúa 10 / x y evita la división por cero. Invertir el orden provocaría un error. Además, && tiene mayor precedencia que ||, así que utilice paréntesis en expresiones mixtas para indicar exactamente su intención.
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).
¿Está x fuera del rango 1–10?
x < 1 || x > 10→truecuandoxes demasiado pequeño o demasiado grande.x = 15→false || true→true(fuera).x = 5→false || false→false(dentro).
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 ||.
Combine condiciones con && (ambos verdaderos), || (al menos uno verdadero) y ! (invertir). Java utiliza evaluación de cortocircuito: &&/|| se detienen una vez conocido el resultado (útil para protecciones como x != 0 && 10/x > 2). Construya una tabla de verdad para lógicas complejas y use paréntesis — && tiene mayor precedencia que ||.
AND of two conditions · AND de dos condiciones
&& is true only when both inputs are true. · && es verdadero solo cuando ambas entradas son verdaderas.
a && b is true when... · a && b es verdadero cuando...
&& (AND) needs both operands true. · && (AND) necesita ambos operandos verdaderos.
In a && b, if a is false, then b is... · En a && b, si a es falso, entonces b es...
&& short-circuits: a false left side makes the result false immediately. · && cortocircuita: un lado izquierdo falso hace que el resultado sea falso inmediatamente.
!(true) evaluates to false. · !(true) se evalúa como false.
! reverses the Boolean value. · ! invierte el valor booleano.
With x = 5, what does x < 1 || x > 10 evaluate to? · Con x = 5, ¿qué devuelve la evaluación de x < 1 || x > 10?
false || false → false (5 is inside 1..10). · false || false → false (5 está dentro de 1..10).
The behavior where && and || stop as soon as the result is known is called ___-circuit evaluation (one word). · El comportamiento donde && y || se detienen tan pronto como se conoce el resultado se llama evaluación de ___-circuit (una palabra).
Short-circuit evaluation stops early. · La evaluación de cortocircuito se detiene anticipadamente.