Skip to main content

Programming in C Chapter III - Boolean Values & Operators

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
are TRUE.

examples.
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

example.
false OR true = true
true OR true = true
false OR false = false

*NOT* - accepts a Boolean Variable and provides the
opposite of that variable.

example.
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.

example.

x=TRUE
y=TRUE
z=TRUE
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:

initial example.

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.

IF x=true
THEN do Y

example in C.

bool x=true;
if (x)
{
// 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.
bool x=true;
if (x)
{
// do Y
}
else
{
// 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)

example.

bool x = true;
bool y = false;
if (x && y)
{
//code
}
else if (x || y)
{
//code
}
else
{
//code
}


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.

example.

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.