Short-circuit evaluation: Difference between revisions

(→‎{{header|SNOBOL4}}: ++ smalltalk)
Line 1,314:
b
</lang>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
 
The <code>and:</code> <code>or:</code> selectors are shortcircuit selectors but in order to avoid
evaluation of the second operand, it must be a block: <code>a and: [ code ]</code> will evaluate
the code only if a is true. On the other hand, <code>a and: b</code>, where b is an expression
(not a block), behaves like the non-shortcircuit and (&amp;). (Same speech for or |)
 
<lang smalltalk>Smalltalk at: #a put: nil.
Smalltalk at: #b put: nil.
 
a := [:x| 'executing a' displayNl. x].
b := [:x| 'executing b' displayNl. x].
 
('false and false = %1' %
{ (a value: false) and: [ b value: false ] })
displayNl.
 
('true or false = %1' %
{ (a value: true) or: [ b value: false ] })
displayNl.
 
('false or true = %1' %
{ (a value: false) or: [ b value: true ] })
displayNl.
 
('true and false = %1' %
{ (a value: true) and: [ b value: false ] })
displayNl.</lang>
 
 
=={{header|SNOBOL4}}==