3.1 Boolean Expressions
How do you write code that tells us whether a user is logged in to our program? Booleans are the solution to these questions.
A boolean refers to a value that is true or false. Those are the only values of a boolean expression, and these are useful if you want to check if something is true or false.
Let’s meet the fellow behind the name, “booleans,” George Boole. Mr. Boole was an English-born mathematician, philosopher, and logician. He introduced boolean algebra in The Mathematical Analysis of Logic (1847) and Investigation of the Laws of Thought (1854). Many consider him to be one of the founders of Computer Science.
George Boole
Let’s start with an example – create a variable, set it equal to true
or false
, then print the value.
You first want to declare the variable, loggedIn
, and initialize its value.
You can similarly set the variable to true instead:
You can imagine a boolean variable as a box that can hold only the values true
or false
. The box below shows the current value of loggedIn
:
Notice that you do not need to have quotations around true
or false
.
The full example looks like:
Comparison operators allow you to compare two values against one another. A comparison returns a boolean result of either true
or false
.
The table below lists each of the common comparison operators and their usages:
Operator | Usage |
---|---|
> |
Greater Than |
< |
Less Than |
>= |
Greater Than Or Equal To |
<= |
Less Than Or Equal To |
== |
Equal To |
!= |
Not Equal To |
A Basic Comparison
In the following example, you compare two variables x
and y
. You store the result of this comparison in variable z
.
What will get printed to the screen? The above comparison, x > y
is evaluating if 10 is greater than 8. Because 10 is indeed greater than 8, z
is assigned a value of true
. Thus, true
will get printed to the screen.
More Practice with Comparisons
Let’s get a little more practice. Take a look at the following code segment below. Pay close attention to each comparison and the operator being used.
In addition to integers, it is possible to compare other data types too. In the example below, the boolean variables are storing the results of a comparison between some of the boolean values computed above.
Suppose you want to write a program that restricts people under a certain height from riding on a roller coaster. For this particular roller coaster, people who are under 4 feet (48 inches) are not allowed on the ride. How would you do this?
After getting the potential rider’s height in inches, you do a comparison to ensure that they are over 48 inches. The result of this comparison is then printed out.
A common mistake is using =
when you actually want to use ==
. =
is used for assignment of variables whereas ==
is used for comparing the equality of two values.
For example, x = 5
stores the value 5 into the variable x. However, x == 5
tests to see if the value 5 is equal to the variable x
and then returns either true or false. They are not the same thing!
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Which of the following is NOT a relational operator?
Ask the user to enter two ints
. Create three boolean
variables, as described below:
The first should be
true
if the first number is greater than the second number.The second should be
true
if the first number is equal to the second number.The third should be
true
if the first number is less than the second number.
Then print the expressions and the result.
Here is an example: