Short-circuit evaluation: Difference between revisions

Content added Content deleted
Line 18: Line 18:
If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
If the language does not have short-circuit evaluation, this might be achieved with nested if statements.


=={{header|PureBasic}}==
Logical '''And''' & '''Or''' operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression.
<lang PureBasic>Procedure a(arg)
PrintN(" # Called function a("+Str(arg)+")")
ProcedureReturn arg
EndProcedure

Procedure b(arg)
PrintN(" # Called function b("+Str(arg)+")")
ProcedureReturn arg
EndProcedure

OpenConsole()
For a=#False To #True
For b=#False To #True
PrintN(#CRLF$+"Calculating: x = a("+Str(a)+") And b("+Str(b)+")")
x= a(a) And b(b)
PrintN("Calculating: x = a("+Str(a)+") Or b("+Str(b)+")")
y a(a) Or b(b)
Next
Next
Input()</lang>
<pre>Calculating: x = a(0) And b(0)
# Called function a(0)
Calculating: x = a(0) Or b(0)
# Called function a(0)
# Called function b(0)

Calculating: x = a(0) And b(1)
# Called function a(0)
Calculating: x = a(0) Or b(1)
# Called function a(0)
# Called function b(1)

Calculating: x = a(1) And b(0)
# Called function a(1)
# Called function b(0)
Calculating: x = a(1) Or b(0)
# Called function a(1)

Calculating: x = a(1) And b(1)
# Called function a(1)
# Called function b(1)
Calculating: x = a(1) Or b(1)
# Called function a(1)</pre>
=={{header|Python}}==
=={{header|Python}}==
Pythons '''and''' and '''or''' binary, infix, boolean operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression.
Pythons '''and''' and '''or''' binary, infix, boolean operators will not evaluate their right-hand expression if the outcome can be determined from the value of the left-hand expression.