Logical Operators NO QUIZ 3 quizzes next class
- Slides: 20
Logical Operators
§ NO QUIZ! § 3 quizzes next class § 1 for midterm day, 1 for today, 1 that’s normally scheduled § Normally scheduled quiz will cover logical operators § Other 2 may cover topics from the midterm § Let’s look over due dates § Review the midterm § Logical Operators § (Work time, if people want) 2
§ It’s yours - you own it! § Make sure that you go over all the questions § Make sure that you’re clear on what the correct answers are § I may take problematic question(s) off the midterm and put them onto the final 3
# People at or below the listed score BIT 116 Midterm 2019 WInter 7 6 5 4 3 2 1 0 60 65 70 75 80 85 90 95 100 105 110 115 Score (1 -150) 120 125 130 135 140 145 150 More 4
Logical Operators: Concepts 5
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. 6
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. Expression && if( karel. front. Is. Clear() karel. get. Direction() == Direction. NORTH) { karel. move(); } var input = $(“#input. Box”). val(); input = parse. Float(input); && if( input > 0 input <= 100) { $(“#msg. Here”). html(“between 0 and 100!”); } Explanation The robot karel should move forwards when karel isn’t blocked by a wall and karel is facing North. Display the message when the user typed in a number that’s strictly greater than zero, and at the same time less than or equal to 100. 7
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. Expression Explanation if(karel. get. Direction() == Direction. NORTH || karel. get. Direction() == Direction. SOUTH) { karel. move(); } if ( karel. front. Is. Clear() && (karel. get. Direction() == Direction. NORTH karel. get. Direction() == Direction. SOUTH) { karel. move(); } var input = $(“#input. Box”. val(); input = parse. Float(input); if( input <= 0 || input > 100) { $(“#msg. Here”). html(“out of range!”); } || ) The robot karel should move forwards when karel is facing North or when karel is facing South The robot karel should move forwards when karel is not blocked by a wall and either karel is facing North or else if karel is facing South Display the message when the user typed in a number that’s out of bounds (either zero or less, or else strictly greater than 100) 8
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. Expression while(!karel. front. Is. Clear()) { karel. turn. Left(); } var input = $(“#input. Box”. val(); input = parse. Float(input); if( ! is. Na. N(input) ) Explanation The robot karel should rotate leftwards until it finds a direction that is not blocked by a wall Display a message when the user typed in a number. (if the user’s input is not “Not A Number”) { $(“#msg. Here”). html(“input IS a number!”); } 9
§ Do something if…. § ALL the criteria are met (if all the conditions are true) AND && § ANY ONE (or more) the criteria are met OR || (if any of the conditions are true) § 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 10
§ Be prepared to report out about this. 11
Logical Operators: Basic Java. Script 12
13
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 14
“use strict” $(document). ready( function() { $("#logical. Ops"). click( function() { var guess = $("#input"). val(); guess = parse. Float(guess); if( is. Na. N( 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 'logical. Ops' 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. 15
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 16
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 */ 17
§ 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 chose. Option. A = false; // Task #1/#2 don't matter since we var finished. Task 1 = false; // didn't choose option 1 var finished. Task 2 = true; // § WRONG: § if( chose. Option. A && finished. Task 1 || finished. Task 2) § Why? § chose. Option. A && finished. Task 1 || finished. Task 2 § false && false || true // AND GOES FIRST § false || true § true 18
§ When evaluating a logical expression AND ( && ) is evaluated before OR ( || ) § Use parentheses to force a different order § RIGHT: § if( chose. Option. A && (finished. Task 1 || finished. Task 2) ) § Why? (finished. Task 1 || finished. Task 2) false && (false || true) // substitute, parens then force || to go first § chose. Option. A && § § false && true § false 19
§ Work on Exercises #2 and #3 for this part of this lecture 20
- Logical operators quiz
- X.next = x.next.next
- Matlab logical operators
- Logical operators in boolean algebra
- Logical operators
- Logical operators calculator
- Logical operators in pl sql
- Operator precedence
- Prolog and or operators
- Logical operators exercises
- Prioritas operator
- And or not operator
- Logical equivalence
- Google search operands
- Logical operators in r
- Rb6tab
- Biconditional proposition examples
- Excel boolean logic
- Logical operators
- Perl not equal
- Unit 9 quiz