4.5.1 How it works
The three operators only accept Boolean values and return a Boolean.
a | b | a And b | a Or b | Not a |
|---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
ProgramCombining conditions
Algorithm Logica
Define edad As Integer <- 15;
Define socio As Boolean <- False;
Write edad >= 12 And edad < 18, " ", Not socio, " ", edad < 12 Or socio;
EndAlgorithmOutput
true true false
The program finishes normally.
4.5.2 Short circuit
And and Or evaluate the left part first. If that is enough to know the answer, the right part is not computed:
False And …is alwaysFalse.True Or …is alwaysTrue.
And, a false left side decides without looking at the right. With Or, a true left side does.At the top, False And a crossed-out division by zero: the result is False without computing the right side. Below, True Or the same division: the result is True.
ProgramThe right side that is never computed
Algorithm Cortocircuito
Write True Or 1 Div 0 = 0, " ", False And 1 Div 0 = 0;
EndAlgorithmOutput
true false
The program finishes normally.
The division by zero is there, but it never runs, so it does not stop the program. The short circuit suppresses the effects and failures of the right side; what it does not suppress is type checking: the right side must be a valid Boolean even when it is not computed.
In short
Andneeds both true;Or, at least one;Notinverts.- They only accept
Boolean. - The right side of
AndandOris only computed when needed.