Programming & Examples

Conditionals in PSeInt: Si, SiNo and Segun with Solved Examples

Decision diamond with true and false branches representing a PSeInt Si statement

Programs become useful the moment they can make decisions: approve or reject a student, apply a discount or not, show an error when the input is invalid. In PSeInt, decisions are written with two structures: Si (if) for yes/no questions and Segun (switch) for choosing among many options. This guide explains both from scratch, shows how to combine conditions with Y, O and NO, and finishes with solved exercises of the kind you will find in exams.

Every example is a complete program — paste it into the PSeInt online editor, run it, and try the Step button to watch which branch is taken.

Conditions: questions with a true or false answer

A condition is an expression whose result is a logical value: Verdadero (true) or Falso (false). You build conditions with relational operators:

OperatorMeaningExample
=equal torespuesta = "si"
<>different fromdivisor <> 0
< / >less / greater thanedad < 18
<= / >=less / greater or equalnota >= 6

When you compare text typed by the user, remember that upper and lower case may be treated as different letters (in our online editor "Si" = "si" is false). Convert answers first with MINUSCULAS and compare them with a lower-case constant.

The simple Si

The simplest form runs a block only when the condition is true:

Algoritmo Descuento
    Definir total Como Real
    Escribir "Total de la compra:"
    Leer total
    Si total > 100 Entonces
        total <- total * 0.9
        Escribir "¡Se aplicó un 10% de descuento!"
    FinSi
    Escribir "A pagar: ", total
FinAlgoritmo

If the total is 150, the discount is applied and the program prints 135. If it is 80, the block is skipped entirely and the program prints 80. Notice that the last Escribir is outside the Si, so it always runs.

Si with SiNo: two alternatives

When there are exactly two paths, add SiNo. Exactly one of the two blocks runs — never both, never neither.

Algoritmo ParImpar
    Definir n Como Entero
    Escribir "Ingrese un número entero:"
    Leer n
    Si n MOD 2 = 0 Entonces
        Escribir n, " es par"
    SiNo
        Escribir n, " es impar"
    FinSi
FinAlgoritmo

n MOD 2 is the remainder of dividing by 2. Even numbers leave remainder 0, odd numbers leave 1. This tiny pattern appears in dozens of exercises.

Nested conditions: more than two alternatives

You can put a Si inside another one. The classic example converts a numeric grade into a description:

Algoritmo Calificacion
    Definir nota Como Real
    Escribir "Nota (0 a 10):"
    Leer nota
    Si nota < 0 O nota > 10 Entonces
        Escribir "Nota no válida"
    SiNo
        Si nota >= 9 Entonces
            Escribir "Sobresaliente"
        SiNo
            Si nota >= 7 Entonces
                Escribir "Notable"
            SiNo
                Si nota >= 5 Entonces
                    Escribir "Aprobado"
                SiNo
                    Escribir "Suspenso"
                FinSi
            FinSi
        FinSi
    FinSi
FinAlgoritmo

The order of the checks matters. Because we test >= 9 first, by the time we reach >= 7 we already know the grade is below 9 — so we don't need to write nota >= 7 Y nota < 9. Checking from the highest threshold down keeps each condition simple.

Indentation is your map. Each FinSi closes the most recent open Si. If you indent consistently, you can see at a glance which SiNo belongs to which Si. The online editor's Format button re-indents your code automatically.

Combining conditions with Y, O and NO

  • Y (and) is true only if both conditions are true.
  • O (or) is true if at least one condition is true.
  • NO (not) inverts a condition.
ABA Y BA O BNO A
VerdaderoVerdaderoVerdaderoVerdaderoFalso
VerdaderoFalsoFalsoVerdaderoFalso
FalsoVerdaderoFalsoVerdaderoVerdadero
FalsoFalsoFalsoFalsoVerdadero

Here is a practical example — a leap year has a precise definition that is a perfect exercise in combining conditions: divisible by 4, except years divisible by 100, unless they are also divisible by 400.

Algoritmo AnioBisiesto
    Definir anio Como Entero
    Escribir "Ingrese un año:"
    Leer anio
    Si (anio MOD 4 = 0 Y anio MOD 100 <> 0) O anio MOD 400 = 0 Entonces
        Escribir anio, " es bisiesto"
    SiNo
        Escribir anio, " no es bisiesto"
    FinSi
FinAlgoritmo

Test it with 2024 (bisiesto), 1900 (no) and 2000 (sí). Those three cases cover every branch of the rule — choosing test values that exercise every path is a habit worth building.

Validating ranges

A very common mistake is writing a range the way we say it in maths: 0 <= x <= 10. That would mean (0 <= x) <= 10 — comparing a logical value with a number — so it is not valid. Always split it with Y:

Si x >= 0 Y x <= 10 Entonces
    Escribir "Dentro del rango"
FinSi

Segun: choosing among many options

When one variable can take several specific values and each needs different actions, a chain of nested Si becomes hard to read. Segun is cleaner:

Algoritmo DiaDeLaSemana
    Definir dia Como Entero
    Escribir "Número de día (1 a 7):"
    Leer dia
    Segun dia Hacer
        1:
            Escribir "Lunes"
        2:
            Escribir "Martes"
        3:
            Escribir "Miércoles"
        4:
            Escribir "Jueves"
        5:
            Escribir "Viernes"
        6, 7:
            Escribir "Fin de semana"
        De Otro Modo:
            Escribir "Número de día no válido"
    FinSegun
FinAlgoritmo

How it works:

  • PSeInt evaluates the expression after Segun once.
  • It compares that value with each option in order and runs the first one that matches.
  • Options can list several values separated by commas (6, 7:).
  • De Otro Modo: runs when no option matches. It is optional, but including it to handle invalid input is good practice.
  • Unlike switch in C or Java, there is no "fall through" and no break: only one branch runs.

Segun compares for equality only. For ranges such as "between 18 and 65" use Si.

A menu-driven calculator

Menus are where Segun shines. Combine it with a Si to protect against division by zero:

Algoritmo Calculadora
    Definir a, b Como Real
    Definir op Como Entero
    Escribir "Primer número:"
    Leer a
    Escribir "Segundo número:"
    Leer b
    Escribir "1) Sumar 2) Restar 3) Multiplicar 4) Dividir"
    Leer op
    Segun op Hacer
        1: Escribir "Resultado: ", a + b
        2: Escribir "Resultado: ", a - b
        3: Escribir "Resultado: ", a * b
        4:
            Si b = 0 Entonces
                Escribir "No se puede dividir entre cero"
            SiNo
                Escribir "Resultado: ", a / b
            FinSi
        De Otro Modo:
            Escribir "Opción incorrecta"
    FinSegun
FinAlgoritmo

Logical variables as flags

Sometimes it is clearer to store the result of a condition in a Logico variable and use it later:

Algoritmo Acceso
    Definir edad Como Entero
    Definir tieneEntrada, puedePasar Como Logico
    Escribir "Edad:"
    Leer edad
    Escribir "¿Tiene entrada? (Verdadero/Falso)"
    Leer tieneEntrada
    puedePasar <- edad >= 18 Y tieneEntrada
    Si puedePasar Entonces
        Escribir "Bienvenido"
    SiNo
        Escribir "Acceso denegado"
    FinSi
FinAlgoritmo

Naming the condition (puedePasar) documents the intent and lets you reuse it without repeating the expression.

Solved exercise: the largest of three numbers

Statement: read three different numbers and show the largest. A neat solution assumes the first is the largest and corrects the assumption when needed — a pattern you'll reuse with arrays later.

Algoritmo MayorDeTres
    Definir a, b, c, mayor Como Real
    Escribir "Ingrese tres números:"
    Leer a, b, c
    mayor <- a
    Si b > mayor Entonces
        mayor <- b
    FinSi
    Si c > mayor Entonces
        mayor <- c
    FinSi
    Escribir "El mayor es ", mayor
FinAlgoritmo

Solved exercise: classify a triangle

Given three side lengths, decide whether they form a triangle and, if so, whether it is equilateral, isosceles or scalene.

Algoritmo TipoTriangulo
    Definir x, y, z Como Real
    Escribir "Longitud de los tres lados:"
    Leer x, y, z
    Si x + y <= z O x + z <= y O y + z <= x Entonces
        Escribir "Esos lados no forman un triángulo"
    SiNo
        Si x = y Y y = z Entonces
            Escribir "Equilátero"
        SiNo
            Si x = y O y = z O x = z Entonces
                Escribir "Isósceles"
            SiNo
                Escribir "Escaleno"
            FinSi
        FinSi
    FinSi
FinAlgoritmo

Note the validation first: checking that the input makes sense before processing it is a mark of a well-designed algorithm.

Common mistakes with conditionals

  1. Forgetting Entonces. Every Si condition must be followed by Entonces.
  2. Missing or extra FinSi. Count them: one per Si. Formatting the code exposes the mismatch.
  3. Overlapping conditions. With separate Si blocks (not nested), several can run. Use SiNo when the options are mutually exclusive.
  4. Comparing decimals for exact equality. Results such as 0.1 + 0.2 may not be exactly 0.3. Compare with a tolerance: ABS(a - b) < 0.0001.
  5. Using a number as a condition. Si x Entonces is invalid when x is numeric; write Si x <> 0 Entonces.

Testing every branch

An algorithm with conditionals has several possible paths, and each one must be tested. A simple technique is to write a small table before running the program: one row per branch, with an input value that should reach it and the output you expect. For the grade example above:

InputExpected outputBranch tested
11Nota no válidaValidation
9.5SobresalienteFirst threshold
7NotableExact boundary
5.5AprobadoThird threshold
2SuspensoFinal SiNo

Boundary values — exactly 7, exactly 5 — are where most bugs hide, because it's easy to write > where the statement says "7 or more". Always include them.

Short-circuit: the order of conditions matters

When combining conditions with Y, put the "safety check" first. In Si divisor <> 0 Y total / divisor > 10 Entonces, the first part protects the division: if the divisor is zero, the whole condition is already false and the division is not needed. Writing safety checks first is a habit that prevents runtime errors in every programming language.

How conditionals look in a flowchart

In a flowchart, a condition is a diamond with two exits labelled true and false that join again after the decision; Segun is a diamond with one exit per option. Paste any example above into the editor and scroll to the flowchart to see it — or read the full PSeInt flowcharts guide.

Practice exercises

  1. Read a temperature in Celsius and print "frío" (below 15), "templado" (15 to 25) or "caluroso" (above 25).
  2. Read a month number and print how many days it has (ignore leap years), using Segun with grouped values.
  3. Read a username and a password and print "Acceso concedido" only if both are correct.
  4. Read the price and quantity of a product; apply 5% discount for 10+ units and 15% for 50+ units.

When you're comfortable, continue with loops in PSeInt — combining loops and conditions is where real algorithms begin.

Frequently asked questions

Does PSeInt have "SiNo Si" (else if)?

Standard PSeInt nests a new Si inside the SiNo block, each with its own FinSi, as in the grade example above. Some editors, including ours, also accept a chained SiNo Si … Entonces sharing one FinSi, but nesting is the portable form.

Can Segun compare text?

Yes, the options can be strings such as "a": or "si":, as long as the expression after Segun is text too.

When should I use Segun instead of Si?

Use Segun when you compare one value against a list of exact options (menus, day numbers, letter codes). Use Si for ranges, combined conditions or anything other than equality.