Logical Patterns 1
Conditionals and control flow
Last updated
Conditionals and control flow
Last updated
In this session we introduce conditional expressions and Boolean algebra. We use those concepts to express arithmetic and logical tests as well as to direct and control the flow of execution. We will approach the exposition of these new ideas using the visual analogies of selecting and filtering geometric objects.
In the construction below we compute the nearest neighbours of a point within a fixed proximity distance sense from a set of random points. The notion of proximity is captured by the distance between points being “less than” a constant value.
This introduces arithmetic conditional operators which include: <
, ≤
, ≥
, >
, =
, ≠
. Note that because some of those do not map directly to keyboard keys we use instead the following equivalent: <=
, >=
, “==
, !=
.
The double equal symbol ==
is often used to differentiate between equality testing and value assignment. Python and many programming languages use this notation. The “expression” component, spreadsheets and many other programming languages use the single =
for equality testing. The inequality symbol is sometimes expressed using <>
e.g. the “expression” component and spreadsheets.
In detail, we use the “populate 2D” component to produce a set of random points within a rectangular region and an on-screen controllable point as the selector. We compute the distances from the control point to all other points and use the “smaller than” component to produce a list of true / false values. Each value is the result of whether a random point is closer than the fixed distance defined by the slider.
The “dispatch” component splits a list in two new lists, given an equally sized list of true / false values. All items of the true category go in one list while the rest in the other. Its use is for filtering lists using a condition aka predicate.
The way the control point adjusts its support points while traversing the random point set is fascinating. To better visualize the proximity criterion we may draw a circle centred about the control node with radius equal to the proximity distance.
It is evident that points falling within the circle are selected using the conditional selection mechanism developed. In other words, we have expressed the concept of testing for point being inside a circle. For visualization purposes we also draw small spheres centred about the selected points and colour-code them.
Practice
Try using the greater and greater or equal than operators to select points outside the range of the control point.
Notice that the equal operator selects nothing, while the not equal selects all points. This is because real numbers have finite resolution. Use the “integer” component after the distance to force dropping the decimal points and convert distances to integers where equality can be computed in an exact sense.
We next duplicate the above setup creating a second control point and its nearest neighbours. The objective here is to select the points that are common to both neighbourhoods. For this we use logic conditional operators. Their use is for allowing us to combine conditions.
Logic AND
In general, for the result of “and” to be true, both A and B must be true. Another way to consider logical “and” is as the intersection of two sets, which is what we have visually captured in the graph.
AND
False
True
False
False
False
True
False
True
Logic OR
The symmetric counterpart of “and” is the “or” operator. We use it here to select the points that fall either within the influence of the first control points or the second.
The truth table for “or” is illustrated below. For example if A is true and B is false, then the result is true. In general, for the result of “or” to be true, either A and B must be true. Another way to consider logical “or” is as the union of two sets.
OR
False
True
False
False
True
True
True
True
Logic XOR
The next Boolean operator of interest is “xor” or “exclusive or”. The goal is to select the points that belong to either control points but not both.
The truth table for “xor” is illustrated below. For example if A is true and B is false, then the result is true. In general, for the result of “xor” to be true, either A and B must be true but not both. The exclusive or operation is fascinating because it behaves sometimes like addition; when the sets don’t overlap all items are selected; but sometimes like subtraction; when the two sets overlap.
XOR
False
True
False
False
True
True
True
False
Logic NOT
The logical “not” operation inverts the a Boolean value or the result of one of the above mentioned operators. It is the last logic or Boolean operation.
The “not” operation unlike the previous is unary as opposed to binary i.e. it requires one operand instead of two. Therefore the truth table is more like a truth row.
NOT
False
True
True
False
“And” is a Boolean operator; an algebra [] using only true / false values, sometimes also denoted as 1 / 0, respectively. Given two possible values i.e. true / false, and two Boolean operands, say A and B, we can express all possible outcomes using a “truth table” seen below. For example if A is false and B is true, then A and B results to false.
This concludes the introduction to Boolean expressions and value. We used Venn diagrams [] to visually convey the core ideas of logic operators. Nevertheless, the verbosity of the graphs produced is rather frustrating.