Skip to content

SpecificationVI · Control flow

6.1

The If decision

If asks a question whose answer can only be True or False, and runs one path or the other depending on the answer. Never both.

Markdown version

6.1.1 How to write it

If «condición» Then
  «instrucciones si es verdadera»
⟦Else
  «instrucciones si es falsa»⟧
EndIf

6.1.2 How it works

Without Else, If is an optional detour. If the answer is True, something extra is done; if it is False, nothing happens and the program carries on.

ProgramDo I take an umbrella?

Algorithm Paraguas
  Define llueve As Boolean <- True;
  If llueve Then
    Write "Llevo el paraguas";
  EndIf
  Write "Salgo de casa";
EndAlgorithm

Output

Llevo el paraguas
Salgo de casa

The program finishes normally.

With Else there are two paths and exactly one is taken. Step through the figure to see what the engine does with this program.

ProgramCan I ride the roller coaster?

Algorithm Montana
  Define estatura As Integer <- 128;
  If estatura >= 120 Then
    Write "Puedes subir a la montaña rusa";
  Else
    Write "Todavía no: vuelve el año que viene";
  EndIf
EndAlgorithm

Output

Puedes subir a la montaña rusa

The program finishes normally.

  1. 1Define estatura As Integer <- 128; runs. The variable appears in memory with the value 128.

    Memory

    estatura = 128

  2. 2The engine reaches the If and evaluates the condition only once: 128 >= 120 gives True.

    Memory

    estatura = 128

    128 >= 120 → True

    Trace

    decision · true branch

  3. 3Since the answer is True, it follows the Then branch and runs its Write.

    Memory

    estatura = 128

    128 >= 120 → True

    Output

    Puedes subir a la montaña rusa

    Trace

    decision · true branch

  4. 4The Else branch is not run: it is not evaluated, writes nothing and does not change memory.

    Memory

    estatura = 128

    128 >= 120 → True

    Output

    Puedes subir a la montaña rusa

    Trace

    decision · true branch

  5. 5At EndIf both paths join. Nothing else remains, so the program ends.

    Memory

    estatura = 128

    128 >= 120 → True

    Output

    Puedes subir a la montaña rusa

    Trace

    decision · true branch

Figure 6.1-1.An If walked through step by step. The diamond is the question; two arrows leave it and only one is followed.

A flowchart with the start, the assignment of estatura, a diamond asking whether estatura is greater than or equal to 120, two boxes with the Write of each branch, the point where they join and the end. Each step highlights the part being run.

6.1.3 More than two paths

PseudoLearn has no special word for "else if". When there are more than two cases, an If goes inside the Else of another, and each has its own EndIf. That way every decision always has exactly two exits.

ProgramThe grade traffic light

Algorithm Semaforo
  Define nota As Integer <- 7;
  If nota >= 9 Then
    Write "Excelente";
  Else
    If nota >= 6 Then
      Write "Aprobado";
    Else
      Write "A repasar";
    EndIf
  EndIf
EndAlgorithm

Output

Aprobado

The program finishes normally.

The questions are asked in order and the first one that answers True wins. That is why order matters: if nota >= 6 were asked first, a 10 would also say "Aprobado". When the cases are concrete values rather than ranges, Switch reads better.

6.1.4 Compound conditions

A condition can combine several questions with And, Or and Not. The engine evaluates them from left to right and stops as soon as it knows the answer. With And, if the left part is False, the right part is never looked at. That lets you protect a dangerous operation.

ProgramDivide only when possible

Algorithm Corto
  Define divisor As Integer <- 0;
  If divisor <> 0 And 10 / divisor > 1 Then
    Write "Grande";
  Else
    Write "No se puede dividir";
  EndIf
EndAlgorithm

Output

No se puede dividir

The program finishes normally.

Since divisor <> 0 is False, the division 10 / divisor is never computed and the program does not stop for dividing by zero.

In short

  • If asks something that can only be answered with True or False.
  • Exactly one path runs; Else is optional.
  • Then and EndIf are required.
  • For more than two cases decisions are nested; the first true question wins.
  • And and Or stop as soon as they know the answer.