Next freebie in 28 Hours*
We are already familiar with Rational Operators, Equality Operators and Explicit Equality Operators. Now it is time to learn Logical Operators. You will understand this easily if you are familiar with the words AND, OR, NAND, NOR, NOT etc. However, there is nothing to worry about even if you are new to these words as we'll discuss everything in detail about the logical operators. In this video we are going to discuss two important logical operators which are commonly used in programming. The first of it is AND, represented by the symbol (&&) and the second is OR, represented by the symbol (||).
The first thing to remember is that this operator is represented by the double ampersand (&&) and not the single ampersand (&). Most people make a mistake here by just typing one ampersand instead of two. So keep in mind that the AND logical Operator is represented by (&&). Now, let's move to the concept of equality once again before we start with the (&&) operator.
In the above exercises, we compared an equation for either equality or inequality. It was very easy to compare any two values. But the problem arises when the equation has more than two variables or when we need to compare more than two equations at a time. To overcome this problem, we use Logical Operators along with the Rational and Equality Operators. For example if we need to compare two equations say c>a and c<b at a time, we can write this equation as trace(c>a && c<b). Now the output is given as true only if both conditions are met: c>a as well as c>b. If either one of the two is false or both are false, the result is given as false. Therefore, && is a logical operators which turns on (true) only if both the conditions are true and not in any other case.
We have seen the need for logical operators in the AND logical operator section, so we won't go deeply into why we need to use the OR logical operator. Just a few important things to note here: first the OR logical Operator is represented by the symbol (||). Secondly, when we use logical operators between any two equations, the result is true if either one of the two statements is true or when both statements are true. For example, if we consider the same example with (||) Operator trace(c>a || c<b), the result is true if at least one of them is true -- or the result is false only when both statements are false. Initially, the compiler checks for the first equation: if it's true then the second equation is not evaluated and the result is given as true. But if the first one is false, the compiler then checks the second equation and gives the result depending on the trueness of the second statement.
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.