Lecture Slides:

advertisement

Logical Operators

 Quizzes!

 Let's look at the schedule

 Logical Operators

2

 Both have been due

 I’m working on grading them – they’ll probably be done next week sometime

3

Logical Operators: Concepts

4

The Logical Operators

The three logical operators allow you to compare two conditional statements to see if one or both of the statements is true and to proceed accordingly. The logical operators can be useful if you want to check on more than one condition at a time and use the results.

Like the comparison operators, the logical operators return either true or false, depending on the values on either side of the operator.

5

The AND Operator:

&&

The logical && operator returns true if the comparisons on both sides of the && operator are true. If one or both comparisons on either side of the operator are false, a value of false is returned.

6

The OR Operator:

||

The logical || operator returns true if the comparison on either side of the operator returns true. So, for this to return true, only one of the statements on one side needs to evaluate to true. To return false, the comparisons on both sides of the operator must return false.

7

The NOT Operator:

!

The logical ! operator can be used on a single comparison to say, “If this is not the case, then return true.”

Basically, it can make an expression that would normally return false return true, or make an expression that would normally return true return false.

8

 Do something if….

 ALL the criteria are met (if all the conditions are true)

ANY ONE (or more)

the criteria are met

(if any of the conditions are true)

AND

&&

OR

||

 You want to

reverse

the true/false value of something

!

NOT

 Use parentheses to indicate the first thing you want to evaluate, the second thing, etc

9

 Be prepared to report out about this.

10

Logical Operators: Basic JavaScript

11

12

Feedback to the user:

 30  that's the secret number!

 Between 20 and 40  it's close, but not right

 Outside the range 1-100  it's out of range

 Input isn't a number  Tell the user that

13

"use strict";

$(document).ready( function() {

$("#logicalOps").click( function() { var guess = $("#input").val(); guess = parseFloat(guess); if( isNaN( guess ) ) {

$("#output").html("You must type a number!"); return;

}

You've seen the above before – when the document is ready to run JS , install an event handler for when the button with the id of 'logicalOps' is clicked . Get whatever the user typed in , convert it to a number , and if it's not a number then tell the user that and stop .

14

} if ( guess < 1 || guess > 100 ) {

$("#output").html("Your guess is out of bounds!"); return;

 The way to check for out of bounds is:

If the number is below the bottom of the range OR above the top of the range

 guess < 1 = below the bottom of the range

 || = OR

 guess > 100 = above the top of the range

 If so then display an error message and stop the function

15

} else if ( guess > 20 && guess < 40 /* && guess != 30 */ ) {

$("#output").html("You're getting closer!"); return;

 The way to check for a number being within bounds is:

If the number is above the bottom of the range AND below the top of the range

 guess > 20 = above the bottom of the range

 && = AND

 guess < 40 = below the top of the range

 If so then display an error message and stop the function

 Note: the comment demonstrates that you can add more criteria if you want

 /* && guess != 30 */

16

 When evaluating a logical expression AND ( && ) is evaluated before OR ( || )

 Example:

You're done when you've chosen option A and then finished either Task #1 or Task #2.

Code: var choseOptionA = false; // Task #1/#2 don't matter since we var finishedTask1 = false; // didn't choose option 1 var finishedTask2 = true; //

 WRONG:

 if( choseOptionA && finishedTask1 || finishedTask2)

 Why?

 choseOptionA && finishedTask1 || finishedTask2

 false && false || true // AND GOES FIRST

 false || true

 true

17

 When evaluating a logical expression AND ( && ) is evaluated before OR ( || )

Use parentheses to force a different order

 RIGHT:

 if( choseOptionA &&

( finishedTask1 || finishedTask2

)

)

Why? choseOptionA &&

( finishedTask1 || finishedTask2

)

  false &&

( false || true

)

// substitute, parens then force || to go first

 false && true

 false

18

 Work on Exercises #2 and #3 for this part of this lecture

19

Download