Logical Operator
<p>We are already familiar with <a href="/courses/video/7/56/Rational-Operators.html">Rational Operators</a>, <a href="/courses/video/7/58/Equality-Operators.html">Equality Operators</a> and <a href="/courses/video/7/199/Explicit-Equality.html">Explicit Equality</a> 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 (||).</p> <h2>AND (&&) Logical Operator</h2> <p>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 <a href="/courses/video/7/55/Equality.html">equality</a> once again before we start with the (&&) operator.</p> <p>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 <a href="/courses/video/5/157/Using-Variables.html">variables</a> or when we need to compare more than two equations at a time. To overcome this problem, we use Logical Operators along with the <a href="/courses/video/7/56/Rational-Operators.html">Rational</a> and <a href="/courses/video/7/58/Equality-Operators.html">Equality</a> Operators. For example if we need to compare two equations say <b>c>a</b> and <b>c<b</b> at a time, we can write this equation as <b>trace(c>a && c<b)</b>. Now the output is given as true only if <b>both</b> conditions are met: <b>c>a</b> as well as <b>c>b</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.</p> <h2>OR (||) Logical Operator</h2> <p>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 <b>either one</b> of the two statements is true or when both statements are true. For example, if we consider the same example with (||) Operator <b>trace(c>a || c<b)</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.</p>