An Introduction to Boolean Logic Development | 9/21/2014|| 4 |
An introduction to Boolean logic, as used in CAOS for C3/DS.
Boolean logic is a kind of algebra used in computer programming that tests whether something is true or false.
There are three main rules with Boolean logic:
Whatever is true, is true.
Whatever is true cannot also be false.
In Boolean logic, there are no shades of gray, there is no maybe, no in-between.
From this, in CAOS, we can set up a number of true-false tests using a conditional (DOIF) statement, and using the modifiers OR, AND and NOT. Conditions can be joined together with AND and OR.
The Venn diagrams below show everything that can exist in the box - the 'universe'. The circles represent certain situations. Red represents our target, and white represents stuff that we exclude.
OR is a term we use when we want to make our true-false test very broad. You can see how it covers situations when either one of the conditions are true, and the area in-between where both situations are true.
AND is a term we use when we want to make our true-false test narrower, because both conditions have to be true for it to work. You can see that only the overlapped area where both conditions are true is highlighted. (e.g. a seed that only sprouts when there is enough soil moisture AND there are fewer than 3 adult plants nearby)
NOT is a term we use when we want to find out when our condition is false. (e.g. a fish critter NOT being in water will suffocate.)
In order for our statement to work, we need to say if this condition is equal to a certain situation, does not equal a certain situation, or if it is greater than a certain situation, or less than a certain situation. A statement which, like OR, gives us a broader definition of 'true' are the commands 'greater than or equal to' and 'less than or equal to'. The symbols below (<>, =, >=, >, <=, < ) and the letters (NE, EQ, GE, GT, LE, LT ) can be used interchangeably.
|is equal to||EQ||=|
|is not equal to||NE||<>|
|is greater than||GT||>|
|is greater than OR equal to||GE||>=|
|is less than OR equal to||LE||<=|
Conditions are read from left to right - unlike in algebra, in CAOS, round brackets don't force the engine to do those tests first. This means that you need to put the first thing first, and 'step along' with the code, doing one check after the other.
Using the ELIF command lets us make another true-false test run directly after the first one has run its course.
It's good practice to put an ELSE command in at the end of a DOIF statement, in case your agent runs up against a situation in which none of your true-false tests apply - and it gives your agent something to do if all else fails.
The last thing to do is to close your DOIF statement with an ENDI.
*Laws of Thought - Wikipedia
*CAOS Chaos | Our Simple Object Un-Simplified!
*Nested DOIF? - Hack Shack
*Conditions - Creatures Wiki
Updated by Malkin on 9/21/2014 - + AND example.
Updated by Malkin on 9/21/2014 - clarify that round brackets don\'t work in CAOS.
The bit about the round brackets only applies to CAOS, though. In other languages, such as C++, the usage of round brackets in boolean constructs usually does work and is extremely helpful at times.