Exercise 28: Boolean Practice

The logic combinations you learned from the last exercise are called "boolean" logic expressions. Boolean logic is used everywhere in programming. It is an essential fundamental parts of computation and knowing them very well is akin to knowing your scales in music.

In this exercise you will take the logic exercises you memorized and start trying them out in Ruby. Take each of these logic problems and write what you think the answer will be. In each case it will be either true or false. Once you have the answers written down, you will start Ruby in your Terminal and type each logic problem in to confirm your answers.

true && true

false && true

1 == 1 && 2 == 1

"test" == "test"

1 == 1 || 2 != 1

true && 1 == 1

false && 0 != 0

true || 1 == 1

"test" == "testing"

1 != 0 && 2 == 1

"test" != "testing"

"test" == 1

!(true && false)

!(1 == 1 && 0 != 1)

!(10 == 1 || 1000 == 1000)

!(1 != 10 || 3 == 4)

!("testing" == "testing" && "Zed" == "Cool Guy")

1 == 1 && (!("testing" == 1 || 1 == 0))

"chunky" == "bacon" && (!(3 == 4 || 3 == 3))

3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun"))

I will also give you a trick to help you figure out the more complicated ones toward the end.

Whenever you see these boolean logic statements, you can solve them easily by this simple process:

Find an equality test (== or !=) and replace it with its truth.

Find each &&/|| inside parentheses and solve those first.

Find each ! and invert it.

Find any remaining &&/|| and solve it.

When you are done you should have true or false.

I will demonstrate with a variation on #20:

3!=4&&!("testing"!="test"||"Ruby"=="Ruby")

Here's me going through each of the steps and showing you the translation until I've boiled it down to a single result:

The more complicated ones may seem very hard at first. You should be able to give a good first stab at solving them, but do not get discouraged. I'm just getting you primed for more of these "logic gymnastics" so that later cool stuff is much easier. Just stick with it, and keep track of what you get wrong, but do not worry that it's not getting in your head quite yet. It'll come.

What You Should See

After you have tried to guess at these, this is what your session with Ruby might look like:

Study Drills

There are a lot of operators in Ruby similar to != and ==. Try to find as many "equality operators" as you can. They should be like < or <=.

Write out the names of each of these equality operators. For example, I call != "not equal."

Play with the Ruby by typing out new boolean operators, and before you press Enter try to shout out what it is. Do not think about it. Shout the first thing that comes to mind. Write it down, then press Enter, and keep track of how many you get right and wrong.

Throw away the piece of paper from 3 away so you do not accidentally try to use it later.

Common Student Questions

Ruby and many languages like to return one of the operands to their boolean expressions rather than just true or false. This means that if you did false && 1 you get the first operand (false) but if you do true && 1 your get the second (1). Play with this a bit.

Is there any difference between != and <>?

Ruby has deprecated <> in favor of !=, so use !=. Other than that there should be no difference.

Isn't there a shortcut?

Yes. Any && expression that has a false is immediately false, so you can stop there. Any || expression that has a true is immediately true, so you can stop there. But make sure that you can process the whole expression because later it becomes helpful.