# 8.5: Booleans


The class Boolean offers a fascinating insight into how much of the Smalltalk language has been pushed into the class library. Boolean is the abstract superclass of the Singleton classes True and False.

Most of the behaviour of Booleans can be understood by considering the method ifTrue:ifFalse:, which takes two Blocks as arguments.

(4 factorial > 20) ifTrue: [ 'bigger' ] ifFalse: [ 'smaller' ]    →    'bigger'


The method is abstract in Boolean. The implementations in its concrete subclasses are both trivial:

Code $$\PageIndex{1}$$ (Squeak): Implementations of ifTrue:ifFalse:

True»ifTrue: trueAlternativeBlock ifFalse: falseAlternativeBlock
↑trueAlternativeBlock value

False»ifTrue: trueAlternativeBlock ifFalse: falseAlternativeBlock
↑falseAlternativeBlock value


In fact, this is the essence of OOP: when a message is sent to an object, the object itself determines which method will be used to respond. In this case an instance of True simply evaluates the true alternative, while an instance of False evaluates the false alternative. All the abstract Boolean methods are implemented in this way for True and False. For example:

Code $$\PageIndex{2}$$ (Squeak): Implementing Negation

True»not
↑false


Booleans offer several useful convenience methods, such as ifTrue:, ifFalse:, ifFalse:ifTrue. You also have the choice between eager and lazy conjunctions and disjunctions.

(1>2) & (3<4)                →    false "must evaluate both sides"
(1>2) and: [ 3<4 ]           →    false "only evaluate receiver"
(1>2) and: [ (1/0) > 0 ]     →    false "argument block is never evaluated, so no exception"


In the first example, both Boolean subexpressions are evaluated, since & takes a Boolean argument. In the second and third examples, only the first is evaluated, since and: expects a Block as its argument. The Block is evaluated only if the first argument is true.

$$\bigstar$$ Try to imagine how and: and or: are implemented. Check the implementations in Boolean, True and False.

This page titled 8.5: Booleans is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.