Conditionals
si (if)
Section titled “si (if)”Evaluates a condition and executes the associated block when it is verdadero (true).
entero edad = 41
// Conditionalssi (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")}sino (else)
Section titled “sino (else)”Optionally add an sino block to handle the opposite case.
si (edad >= 18) { consola.mostrar("Is an adult")} sino { consola.mostrar("Is a minor")}sino si (else if)
Section titled “sino si (else if)”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")}Conditionals in Expressions
Section titled “Conditionals in Expressions”You can use the ternary operator for simple conditionals.
texto estado = edad >= 18 ? "Adult" : "Minor"Strict Comparisons
Section titled “Strict Comparisons”- 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")}Best Practices
Section titled “Best Practices”- Group complex conditions with parentheses to improve readability.
- Use methods like
contiene_claveorlongitud()before evaluating conditions on JSON or lists. - Extract conditions to descriptive functions when they repeat in multiple places.