If you missed lesson #2, you can take a look at it here.
Today’s lesson will cover the concept of logical operators, what they are and how to use them. The logic behind them is very simple. No pun intended they are called logical operators after all. View them as simple electrical circuits. There are three possible outcomes based on which current will flow through the hypothetical wires.
Again with the operators…
Last lesson we covered what operators and expressions are. Operators, just as their names state, operate on one or two values. These values must eventually be equal to a Boolean value, a simple
false. Logical operators are, in turn, very similar to the arithmetic operators we learned about last lesson. The way they behave is at the logical level almost the same. The key exception being on what types of values the operators can be applied to.
Logical operators can be applied to Boolean values themselves. Meaning, they are used to reason with Booleans. Let’s say you want to make sure both
5 is greater than
7 is less than
9. Only if both of these expressions hold
true will the logical operation hold
true. In another case, let’s say you want to check whether
1 is equal to
7 is equal to
7. It’s perfectly fine if only one of the two arithmetic expressions holds
true, the logical operation will hold
&& operator represents logical AND. This is a binary operator, and its result is
true only if both the values given to it are
true. By being a binary operator it takes two values.
true && false // → false true && true // → true
This logical operator is denoted by the special symbol called pipe. The pipe symbol is written like this
||. Logical OR will produce
true if either of the values given to it is
false || true // → true false || false // → false
NOT is very interesting. All it does is switching the value it is given. It’s denoted by an exclamation mark, like this
!true // → false !false // → true
This may seem funny at first, but the power of this operator is incredibly useful. You can negate whole values and expressions, and even validate their existence. This comes in handy a lot.
When you mix operators, how do you know which will be executed first? Well, with arithmetic it’s easy, you just follow the logic of math you learned in primary school. Multiplication and division first, then addition and subtraction. But what now? Logical operators don’t exist in arithmetic. The use of operators boils down to something called precedence. Operator precedence determines which operator will be evaluated first, meaning the ones with higher precedence are the ones to be evaluated first.
Logical OR (
||) has the lowest precedence, then comes logical AND (
&&), then the comparison operators (
>, ==, <, !=, ...), then the rest.
Here’s an exercise for you to solve:
1 + 1 == 2 && 10 * 10 > 50 // what is the value of this expression?
true ? 1 : 2 // → 1 false ? 1 : 2 // → 2
When it is
true, the middle value is chosen, and when it is
false, the value on the right comes out.
Great work! Logical operators are the core building blocks of any program. These expressions are used to check the validity in many parts of our programs. The
false value dictates whether the program will continue executing or follow a totally opposite flow of control. These concepts will all be crystal clear to you in a few lessons when we get into them in more detail. Join me in the next lesson when I’ll be writing about special values, and precise type comparisons
Next article coming soon…
Hope you guys and girls had as much fun reading this article as I had writing it!
Drop an upvote if you liked it on steemit or share if you believe it will be of help to someone.
Feel free to ask any questions you have here.