Boolean Expressions and IF


Sample of if inside of if:

if (badHairDayBoolean) {
    if (seriousHangoverBoolean) 
       document.write("Reality Bytes, Stay in Bed Today");
}

Sample of using ANDsame as above

if (badHairDayBoolean && seriousHangoverBoolean)
{ 
    document.write("Reality Bytes, Stay in Bed Today"); 
}

These are the same because, in the first example, JavaScript can only get in to check the second condition if the first is true. It can then only get inside to the write if the second condition is true also. This is the same way and works; both conditions must be true for the whole condition to be true.


Grades

When we specifiy a grading scheme in English, we typically say, "If you score higher than 90 you will receive an A. If you score higher than 80, a B. Higher than 70, a C. Higher than 60, a D. And, if you score 60 or below, an F."

English is too imprecise for programming: Javascript example

In this example, each if is checked independently. That is like an OR operation.

Ranges of values example

Here we are being more precise, "If you score between 80 and 90, you will receive a B". Or even more precisely, "If you score higher than 80, but not higher than 90, you will receive a B".

if (average > 90)
  document.write("You earned an A<br>");
if (average > 80 && average <= 90)
  document.write("You earned a B<br>");
if (average > 70 && average <= 80)
  document.write("You earned a C<br>");
if (average > 60 && average <= 70)
  document.write("You earned a D<br>");
if (average <= 60)
  document.write("You earned an F<br>");

More Boolean Expressions