Five Minute C# — Lesson 4
Boolean operators and conditional statements
Lessons: 1 ・ 2 ・ 3 ・ 4 ・ 5 ・ 6 ・ 7 ・ 8 ・ 9 ・ 10 ・ 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.
variable_a = true;
variable_b = 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 (==)
2 == 2;
Not Equal to (!=)
3 != 2;
Less than (<)
2 < 3;
Less than or equal to (<=)
2 <= 3;
Greater than (>)
3 > 2;
Greater than or equal to (>=)
3 >= 2;
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?
bool1 = 1 < 2;
That’s right, it’s true.
bool1 = true;
How about this one?
bool2 = 100 == (2*50);
That’s also true.
bool2 = true;
And finally…
bool3 = -22 >= -18;
That’s false.
bool2 = 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 (&&).
a && b
The OR operator checks if at least one of the statements is true (||).
a || b
The negation operator returns the inverse of the statement (!).
!(a && b)
!(a || b)
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.
bool1 = (1 < 2) && (2 < 3)
bool1 = true;
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.
bool2 = (1 < 2) && (2 > 3)
bool2 = false;
Unlike the AND operator, the OR operator (||) only checks if one side satisfies the value of true.
bool2 = (1 < 2) || (2 > 3)
bool2 = 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.
bool3 = !((3*4) < (4*3))
bool3 = true;
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.
bool1 = false || !(true && true);
bool2 = false && !(true ||true);
If there’s any doubt, just bind your expressions with parenthesis () to ensure that they are prioritised in the evaluation order.
bool2 = false && (!(true ||true));
Well done! That was a heavy one. But it’ll get easier from here on.