<p>Inside the parenthesis of the <code>if</code> statement must be a boolean expression. This is an expression that evaluates to either <code>true</code> or <code>false</code>. We can do more complex boolean expressions through logical operators.</p>
<h2>Logical Operators</h2>
<p>NOT <code>!a</code> this is true when <code>a</code> is false</p>
<p>AND <code>a && b</code> this is true when both operands are true</p>
<p>OR <code>a || b</code> this is true when either a is true OR b is true</p>
<h2>Truth Tables</h2>
<ul>
<li>Show all possible outcomes</li>
<li>It breaks the expression down into parts</li>
</ul>
<h3>Not</h3>
<p>Let's look at the most simplest case. Not.</p>
<table>
<thead>
<tr>
<th>a</th>
<th>!a</th>
</tr>
</thead>
<tbody>
<tr>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
</tr>
</tbody>
</table>
<h3>AND</h3>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>a && b</th>
</tr>
</thead>
<tbody>
<tr>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
</tbody>
</table>
<p>Notice here that <code>a && b</code> is only true when both <code>a</code> and <code>b</code> are true.</p>
<h3>OR</h3>
<table>
<thead>
<tr>
<th>a</th>
<th>b</th>
<th>a \</th>
<th>\</th>
<th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>true</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
</tbody>
</table>
<p>Notice here that <code>a || b</code> is only false when both <code>a</code> and <code>b</code> are false.</p>
<h2>Distributive Property of Logical Operators</h2>
<p>The following statements are equivalent</p>
<p><code>!(a && b)</code> is equivalent to <code>!a || !b</code></p>
<p>Notice how when you distribute the <code>!</code> you have to flip the operand as well. <code>&&</code> becomes <code>||</code></p>
<p>Same is true for the following example</p>
<p><code>!(a || b)</code> is equivalent to <code>!a && !b</code></p>
<p><code>!(a || b && c)</code> is equivalent to <code>!a && (!b || !c)</code></p>
<h2>Short Circuit Evaluation</h2>
<p>In an <code>&&</code> (AND) statement, if the left side is <code>false</code>, there is no need to evaluate the right side. Since it's going to be false anyways!!</p>
<pre><codeclass="language-java">false && true; // FALSE no matter what the right side is</code></pre>
<p>In an <code>||</code> (OR) statement, if the left side is `true, there is no need to evaluate the right side. Since it's going to be true by default!!</p>
<pre><codeclass="language-java">true || false; // TRUE no matter what the right side is</code></pre>
<p>Java takes this shortcut by default for efficiency reasons</p>