Today we will learn a bit about Boolean values, operators and expressions.
Boolean values and conditions are named after 19th century mathematician and logician George Boole who pioneered a field of logic now referred to as Boolean logic; which is based upon grouping and comparing *Boolean values*.
*Boolean Value* - a variable that has two possible conditions; TRUE and FALSE.
Similar to a light switch that can be either on or off, or how binary
numbers can be either 1 or 0.
Boolean values are seem fairly simply on the surface. However, they allow for a dynamic array of combined values that allow for nearly infinite complexity.
*Boolean Operator* - A Boolean Operator combines two Boolean values into a single value. The two most common of such operators are AND and OR, but there are quite a few additional Operators we will explore as well.
*AND* - results in a value of TRUE ONLY if BOTH input values
false AND true = false
false AND false = false
true AND true = true
*OR* - results in a value of TRUE if EITHER of the
operator's input values are TRUE
false OR true = true
true OR true = true
false OR false = false
*NOT* - accepts a Boolean Variable and provides the
opposite of that variable.
NOT true = false
NOT false = true
Combining variable and operators into a single statement results in a *Boolean Expression*. Boolean Expressions can be *nested* to provide more complex outcomes. As numbers are prioritized and grouped using the arithmetical order of operations (PEMDAS), Boolean expressions can be grouped using paranthesis.
x AND (y OR (NOT z)) ---> this is 3 expressions
x AND (y OR FALSE) ---> evaluate the innermost expression 1st
x AND TRUE ---> finally we reach a tautology
TRUE AND TRUE = TRUE ---> resolving the nested expressions as
TRUE (all Boolean expressions
necessarily resolve to tautologies or contradictions)
Next, let us review how these nested expressions can be used in a programming language. In C, the syntax for Boolean expressions is different than simply using the words AND, OR and NOT. The translation is as follows:
AND = &&
OR = ||
NOT = !
The nested expressions from our previous example can be translated into C rather simply:
x AND (y OR (NOT z))
translated to C.
x && (y || (!z))
Now that we know how to translate Boolean expressions into C, how do we use it? Consider the example of a script that should execute only if a given variable is TRUE. For this purpose, nearly all programming languages provide support for the IF condition.
example in pseudocode.
THEN do Y
example in C.
// do Y
What if we wish to provide an alternative branch, to assign the script a set of instructions for when x IS NOT true? In these circumstances, we would append ELSE to our IF condition (forming an IF...ELSE condition)
example in C.
// do Y
// do Z
Another important function is the ELSE IF condition. Using else if, we can branch application behavior in more than two directions.
Suppose you have two Boolean values you would like your script to consider, called X and Y. We want our program to do one thing if X and Y are both TRUE. We want the program to do another thing if either X or Y are TRUE. And finally, we want the program to do a third thing for every other combination of truth statements for X and Y not covered in the first two conditions.
We can express such an application in C using the following code and relying on ELSE IF (in this example we declare X and Y to be TRUE and FALSE, respectively)
bool x = true;
bool y = false;
if (x && y)
else if (x || y)
Working with variables in this manner is useful, but we appear to be limited to only a few conditions. Booleans become much more powerful when we introduce *comparisons*. *Comparisons* are ways to evaluate values that are not originally declared as Boolean.
To see if two values are the same, we use: ==
== returns TRUE if the values are equal and FALSE if they are not.
Other common comparisons are:
LESS THAN : <
GREATER THAN : >
LESS THAN OR EQUAL TO : <=
GREATER THAN OR EQUAL TO : >=
Using one last concrete example, let's combine ALL of the topics related to Boolean values we have covered so far, as well as a few topics from our previous discussions as well.
Suppose we have two variables: `temperature` and `isHungry`. `temperature` is a floating point number that can have decimal places. This example is a very simple program to tell someone what to eat based on the temperature.
if (hungry && (temperature >= 100))
printf("eat ice cream");
else if (hungry && (temperature <= 0))
printf("eat spicy food");
else if (!hungry)
printf("don't eat anything");
That's all for today's lesson on Boolean. Try writing your own program using a Boolean function in a loop, such that when the the Boolean value changes, the loop terminates.
Earlier this week a young government contractor named Reality Winner was accused by police of leaking an internal NSA document to news outle...
UPDATE March 1st, 2017 : I'm glad to see that people are finding this helpful, and thanks to everyone that has contacted me here or via ...
The Problem The configuration of the system when this error was encountered is as follows: A. Windows Server 2008 R2 Redundant Domain Co...
So it turns out that setting your AWS EC2 server's hostname to be persistent across reboots is a surprising pain in the ass, at least wi...