Comparación y Lógica
En esta página
Los seis operadores de comparación siempre devuelven bool. Para tipos
primitivos, == compara por valor; para arrays y structs, la comparación es
estructural (profunda). Las cadenas usan orden lexicográfico. Zolo no
admite encadenamiento estilo Python como a < b < c — combina con &&
cuando necesites comprobar un rango.
Igualdad, orden, floats (IEEE-754), cadenas y arrays — todos devuelven bool.
// Feature: Comparison operators
// Syntax: `==`, `!=`, `<`, `<=`, `>`, `>=`
// When to use: comparing values. Always return `bool`. In Zolo,
// `==` compares by value for primitive types and structurally for
// structs/arrays (deep equality).
// Equality and inequality.
print(1 == 1) // true
print(1 == 2) // false
print(1 != 2) // true
print("alice" == "alice") // true
print("alice" != "bob") // true
// Order.
print(3 > 2) // true
print(3 >= 3) // true
print(1 < 5) // true
print(1 <= 1) // true
// Floats.
print(1.5 < 1.6) // true
print(0.1 + 0.2 == 0.3) // false (IEEE-754!)
// Strings — lexicographic comparison.
print("apple" < "banana") // true
print("Z" < "a") // true (ASCII: Z=90, a=97)
// Arrays — structural comparison.
print([1, 2, 3] == [1, 2, 3]) // true
print([1, 2, 3] == [1, 2, 4]) // false
// Chaining via `&&` (Zolo does NOT support `a < b < c` like Python).
let n = 5
print(0 < n && n < 10) // true
Los operadores lógicos &&, || y ! combinan valores booleanos. && y
|| usan evaluación en cortocircuito: el lado derecho solo se evalúa
cuando el lado izquierdo no resuelve por sí solo. La precedencia es
! > && > ||.
Cortocircuito demostrado con una función con efecto secundario y precedencia entre !, && y ||.
// Feature: Logical operators
// Syntax: `&&` (and), `||` (or), `!` (not)
// When to use: combine booleans in conditionals. `&&` and `||`
// short-circuit (the 2nd expression is NOT evaluated if the 1st
// already decides). `!` flips a bool.
// AND — true only if both are true.
print(true && true) // true
print(true && false) // false
print(false && true) // false
// OR — true if at least one is true.
print(true || false) // true
print(false || false) // false
// NOT — flips.
print(!true) // false
print(!false) // true
print(!(1 == 2)) // true
// Short-circuit: `||` stops on the first true.
fn side_effect(name: str) -> bool {
print("evaluated: {name}")
return true
}
let _r = true || side_effect("right") // "right" is NEVER evaluated
print("---")
// `&&` stops on the first false.
let _r2 = false && side_effect("right") // "right" is NOT evaluated
print("---")
// Precedence: `!` > `&&` > `||`.
print(true || false && false) // true (= true || (false && false))
print(!true || true) // true (= (!true) || true)
// Combining with comparisons.
let age = 25
let has_id = true
print(age >= 18 && has_id) // true
print(age < 13 || age > 65) // false
Desafío
En el ejemplo lógico, cambia let _r = true || side_effect("right") por
let _r = false || side_effect("right") y observa cuándo se evalúa el efecto
secundario.
Consulta también