Five Minute C# — Lesson 4

Boolean operators and conditional statements

Alexander Laurence
Five Minute C#
5 min readOct 28, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

Did you hear the joke about Boolean operators?

It’s funny because it’s true!

You can tell I’m in a good mood because here’s where we stop all the boring work about numbers and move onto something a bit more fun: bools!

Many times when we are programming, we may need to declare if a variable is true or not. We refer to this particular data-type as a boolean (or bools for short). Booleans are defined by taking binary (one of two) values or keywords, these are true and false.

If you want to dig deep. A Boolean is actually a special type of integer which can take either a value of 1 or 0. So a value of true will be 1, and a value of false will be 0.

But this doesn’t mean they can be operated on in the usual way as with integers. For example you can’t add a true variable with a false variable. Think of them more as statements. The reason why we need Booleans will become apparent later in this lesson when we learn about conditional statements.

For now, let’s touch a bit on control flow comparators. There are 6 of them!

Equal to (==)

Not Equal to (!=)

Less than (<)

Less than or equal to (<=)

Greater than (>)

Greater than or equal to (>=)

Did you notice how we used a double equals (==) rather than a single equals (=) operator? That’s because == compares whether two things are equal, whereas = assigns a value to a variable.

Now let’s introduce boolean values to these comparators. What boolean value will this variable take?

That’s right, it’s true.

How about this one?

That’s also true.

And finally…

That’s false.

Good! It’s a bit like a quiz right? Now let’s see how deep the rabbit hole goes…

If we want to compare statements and display their boolean values, we will need to use boolean operators.

There’s 2 of these main conditional operators: AND Operator and the OR Operator. You can also include the negation operator, which returns the inverse the operation (often called a ‘not’ operator).

The AND operator checks if both statements are true (&&).

The OR operator checks if at least one of the statements is true (||).

The negation operator returns the inverse of the statement (!).

That’s all nice, but how do we apply this knowledge to what we already know so far?!

The boolean value only returns true if both sides of the AND expression (&&) return true. Otherwise, it will return false. This is very important.

Whereas in here, bool2 will return false since one of the expressions is false. In this case, 2>3 is false. So the whole bool will be deemed false.

Unlike the AND operator, the OR operator (||) only checks if one side satisfies the value of true.

So bool2 would be true if we use an OR operator (||), but false if we use an AND operator (&&). This can be quite confusing if this is your first time dabbling into conditionals. So please feel free to practice for yourself, and don’t hesitate to go back if you missed anything.

Lastly, we land on the negation boolean operator (!). This special type of operator returns true for all false statements, and false for all true statements. Huh?

Let’s take a look at an example.

Both sides of the expression are 12, so there’s no way that 12 is less than 12. Therefore the statement that 12 is less than 12 is false, is true!

I hope that simplifies things! But before we close off, there’s a couple more important things to touch upon.

A bit like mathematics, there is an order of operation with boolean operators. not is evaluated first, AND is evaluated next, and OR is evaluated last. This is referred to as precedence. This can be very important when you have more complicated boolean values.

If there’s any doubt, just bind your expressions with parenthesis () to ensure that they are prioritised in the evaluation order.

Well done! That was a heavy one. But it’ll get easier from here on.

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.