Computer Science 161.01

Professor Valente

 

October 14, 2019 (continued)

 

Boolean Expressions in JavaScript

 

As I mentioned, a boolean expression is an expression that evaluates to true or false.

 

We can very quickly create some by comparing numbers in the usual way.

For example 7 > 5 is an expression whose value is true.

But 7 < 5 is an expression whose value is false.

Interestingly,  3 == 3 is true so evidently == means “is equal to”.

 

You’ll discover also that 3 != 3 is false but 3 != 4 is true.

So what do you think != means?

 

All told, the comparison operators are > , >= , < , <= , == , !=

and they denote respectively “greater than”, “greater than or equal to”, “less than”, “less than or equal to”, “is equal to”, “not equal to”.

 

Let’s mess around with more boolean expressions. Recall that we studied operations such as AND, OR, and NOTwhen we studied logical operations.  See if you can figure out which of the strange symbols below denote AND, OR, and NOT.

 

Enter each of the expressions below separately in the JavaScript scratchpad site

and click the Evaluate button.  See if the answer at the bottom is what you would have predicted.  

 Be sure to delete each expression before entering a new one.

 

      7 > 5

      1+1==3

      7>5 && 1+1==3

     

      !(7>5 && 1+1==3)

     

      20%6 == 2 || 1 >= 10

 

      true && false

     

      PASTE ALL THREE OF THESE INTO THE SCRATCHPAD, THEN EVALUATE

      itsFriday = false; 

      itsMonday = true;   

      itsMonday && !itsFriday 

 

 

In the last segment, you witnessed assignment statements for the first time.

Assignment statements simply do what STORE did on the VSC-32.

They STORE a value into some memory location (i.e. they assign a value to the location).

 

itsMonday = true;         ASSIGNS (or STOREs) true to location named itsMonday 

itsFriday = false;        ASSIGNS false to location named itsFriday 

 

 

Obviously, you can assign an integer value to a location as well.

 

X = 20;

Y = 30;

Z = X + Y - 5;

Z

 

The above statements do the following (in VSC-32 terms)

      Assign 20 to (or STORE 20 into) X

      Assign 30 to Y

      LOAD X, ADD Y, SUBTRACT a 5, STORE Z

      WRITE Z

 

I hope when you paste the assignment statements into the JavaScript scratchpad site

you’ll see that Z has the value 45.

 

     

EXERCISE: Write a Boolean expression that tests whether grade is in the range 0 to 100 (inclusive).

You might check back to the previous page to see a brief mention of this example.

 

When you think you have written the expression properly, enter it as the 2nd line following the line grade=75;

 

grade=75;

//Put your Boolean expression here that tests whether grade is valid

           

 

//Then click on Evaluate to see whether you get true or false

 

You should try values other than 75 too, including bizarre ones!

 

When all is working well, replace

grade=75;

with

grade=prompt("Enter your test grade:");

 

Have fun with that!

 

 

MORE ABOUT ASSIGNMENT STATEMENTS AND I/O on WEDNESDAY!