Skip to content

Conditionals

Evaluates a condition and executes the associated block when it is verdadero (true).

entero edad = 41
// Conditionals
si (edad > 60) {
consola.mostrar("The person is a senior")
} sino si (edad > 18) {
consola.mostrar("The person is an adult")
} sino {
consola.mostrar("The person is a minor")
}

Optionally add an sino block to handle the opposite case.

si (edad >= 18) {
consola.mostrar("Is an adult")
} sino {
consola.mostrar("Is a minor")
}

Allows chaining additional conditions.

si (edad > 60) {
consola.mostrar("The person is a senior")
} sino si (edad > 18) {
consola.mostrar("The person is an adult")
} sino {
consola.mostrar("The person is a minor")
}

You can use the ternary operator for simple conditionals.

texto estado = edad >= 18 ? "Adult" : "Minor"
  • Use == to compare values.
  • For JSON and lists, the complete structure is compared.
  • Avoid comparing different types without prior conversion; otherwise an error will be generated.
jsn usuario = { nombre: "Ana", activo: verdadero }
si (usuario.activo == verdadero) {
consola.mostrar("Active user")
}
  • Group complex conditions with parentheses to improve readability.
  • Use methods like contiene_clave or longitud() before evaluating conditions on JSON or lists.
  • Extract conditions to descriptive functions when they repeat in multiple places.