6.1.1 How to write it
If «condición» Then
«instrucciones si es verdadera»
⟦Else
«instrucciones si es falsa»⟧
EndIf6.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";
EndAlgorithmOutput
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
EndAlgorithmOutput
Puedes subir a la montaña rusa
The program finishes normally.
1
Define estatura As Integer <- 128;runs. The variable appears in memory with the value 128.Memory
estatura= 1282The engine reaches the
Ifand evaluates the condition only once:128 >= 120givesTrue.Memory
estatura= 128128 >= 120→TrueTrace
decision · true branch
3Since the answer is
True, it follows theThenbranch and runs itsWrite.Memory
estatura= 128128 >= 120→TrueOutput
Puedes subir a la montaña rusa
Trace
decision · true branch
4The
Elsebranch is not run: it is not evaluated, writes nothing and does not change memory.Memory
estatura= 128128 >= 120→TrueOutput
Puedes subir a la montaña rusa
Trace
decision · true branch
5At
EndIfboth paths join. Nothing else remains, so the program ends.Memory
estatura= 128128 >= 120→TrueOutput
Puedes subir a la montaña rusa
Trace
decision · true branch
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
EndAlgorithmOutput
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
EndAlgorithmOutput
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
Ifasks something that can only be answered withTrueorFalse.- Exactly one path runs;
Elseis optional. ThenandEndIfare required.- For more than two cases decisions are nested; the first true question wins.
AndandOrstop as soon as they know the answer.