Logical Operators.  Quizzes!  Let's look at the schedule  Logical Operators 2.

Slides:



Advertisements
Similar presentations
TO YOUR LAPTOP How to Add a Printer Lesson 3 – November 18, 2013 – Michelle Lowe.
Advertisements

Microsoft® Small Basic
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Main task -write me a program
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
Visual Basic Chapter 1 Mr. Wangler.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Xcode testing Using XCTest.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
JavaScript Lecture 6 Rachel A Ober
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
® IBM Software Group © 2006 IBM Corporation JSF Progress Bar This Learning Module shows how to integrate EGL/JSF functionality into a run-time progress.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
® Microsoft Access 2010 Tutorial 5 Creating Advanced Queries and Enhancing Table Design.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Controlling Program Flow with Decision Structures.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Checking If User Input Is Numeric.  Quiz  Detecting numeric input  Finish Prior Lecture  Y'all work on one of the problems listed 2.
Conditionals.
Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.
Computer Science Up Down Controls, Decisions and Random Numbers.
Decomposing A Program Into Functions. 2 3 Program/Function Decomposition.
JavaScript: Conditionals contd.
BIT116: Scripting Lecture 05
Exsys Developer Tutorial
Discussion 4 eecs 183 Hannah Westra.
JavaScript, Sixth Edition
Visual Basic.NET Windows Programming
Introduction to Decision Structures and Boolean Variables
The Ohio State University
Repetition Structures
CERNER MILLENNIUM Diagnoses and Problems
IF statements.
Retrieving information from forms
Form Validation and AJAX
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Lesson 9: "if-else-if" and Conditional Logic
Week#2 Day#1 Java Script Course
Lecture 07 More Repetition Richard Gesick.
Starter Write a program that asks the user if it is raining today.
Microsoft Visual Basic 2005 BASICS
More Selections BIS1523 – Lecture 9.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Together Let’s Design an Online Quiz
Lesson 8: Boolean Expressions and "if" Statements
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
Programming in JavaScript
Tonga Institute of Higher Education
Coding Concepts (Basics)
Programming in JavaScript
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Learning Intention I will learn about selection with multiple conditions.
How to allow the program to know when to stop a loop.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Retrieving information from forms
Presentation transcript:

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

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.

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.

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.

 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 9

 Be prepared to report out about this. 10

11 Logical Operators: Basic JavaScript

12

Feedback to the user:  30  that's the secret number!  Between 20 and 40  it's close, but not right  Outside the range  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 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