JavaScript: Conditionals contd.

Slides:



Advertisements
Similar presentations
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Advertisements

PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript – If/Else contd
JavaScript Controlling the flow of your programs with ‘if’ statements
Precedence Operators Error Types
GCSE COMPUTER SCIENCE Practical Programming using Python
Chapter 10 Programming Fundamentals with JavaScript
User-Written Functions
Regular Expressions 'RegEx'.
Department of Computer Engineering
Content Programming Overview The JVM A brief look at Structure
CS0007: Introduction to Computer Programming
IT 130 – Internet and the Web - Yosef Mendelsohn
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loops BIS1523 – Lecture 10.
Programming Mehdi Bukhari.
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
IF statements.
Variables, Expressions, and IO
Retrieving information from forms
CMSC201 Computer Science I for Majors Lecture 03 – Operators
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 10 Programming Fundamentals with JavaScript
More Selections BIS1523 – Lecture 9.
Chapter 2 - Introduction to C Programming
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Variables ICS2O.
Chapter 2 - Introduction to C Programming
Repetition Structures
CISC101 Reminders Assn 3 due tomorrow, 7pm.
T. Jumana Abu Shmais – AOU - Riyadh
CMSC 202 Java Primer 2.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
We’re moving on to more recap from other programming languages
Introduction to TouchDevelop
Java Programming Control Structures Part 1
Logical Operations In Matlab.
Chapter 2 - Introduction to C Programming
Conditional Execution
Relational & Logical Operators, if and switch Statements
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
'Boolean' data type.
JavaScript objects, functions, and events
Relational Operators.
Using Decision Structures
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Chapter 2 - Introduction to C Programming
Introducing JavaScript
Chapter 3: Selection Structures: Making Decisions
Making decisions with code
Javascript Chapter 19 and 20 5/3/2019.
CISC101 Reminders Assignment 3 due today.
Introduction to C Programming
Controlling Program Flow
Web Programming and Design
Computers and Scientific Thinking David Reed, Creighton University
Control Structures.
Retrieving information from forms
Presentation transcript:

JavaScript: Conditionals contd. Comparing for equality Logical And/Or

Learning Objectives By the end of this lecture, you should be able to: Describe the difference between the assignment operator = and the equality operator == Describe the use and application of the logical ‘OR’ operator: || Describe the use and application of the logical ‘AND” operator: &&

Relational operators The relational operators allow us to test the relationship between two items. Often this involves numbers (e.g. age>=65), but we also frequently may want to compare two Strings such as to see if a user’s password matches the password on file. Inequality >, >=, <, <= Not equal != Equal == We haven’t discussed these last two yet… 3

Comparing for ‘equality’ We’ve discussed several examples comparing items for greater than or less than. What about comparing two items to see if they are the same? Example: Prompt the user for the day of the week. If it’s a Sunday, print: “No need to pay the meter!” var day; day = document.getElementById('txtDay').value; //Note: No ‘parse’ here since 'day’is not a quantity! if (day == "Sunday") { alert("No need to pay the meter!"); } ***** Note the comparison operator ‘ == ‘ . If you want to compare two values, you need to use ‘==‘. Using one equals sign ‘=‘ will NOT work!

‘=‘ vs ‘==‘ Don’t confuse these! The equals symbol ‘=‘ is the operator used to assign some value to a variable. For example: var age = 34; //this is an “assignment” If we want to compare values, we can not use ‘=‘ because, again, the equals operator assigns. That is, the ‘=‘ operator does not compare! If we want to compare, we use the (very similar) operator: ‘==‘ if (name == “Bob”) …. if (age == 65) …. if (birthday == todayDate) ….

So, don’t forget… The moral is that whenever you are trying to compare a variable to a value, you should check carefully check to make sure your if and if-else statements only use the equality operator: == Typing = instead of == will nearly always produce unexpected behavior. In addition, in a lengthy and/or complex program, this error can sometimes be quite difficult to track down! 6

if (day == "Sunday") Why did we need quotes? Supposing we had instead written: if (day == Sunday) Can you spot the problem? In this case, JavaScript would think that Sunday was a variable. By placing the word ‘Sunday’ inside quotations, we are telling JavaScript that we are comparing the variable called ‘day’ with a String of text (value of ‘Sunday’)

day = document.getElementById('txtDay').value; if (day == "Sunday") { If the day is a Sunday, tell them they don’t have to feed the meter. If it is not Sunday, say: “Better get some quarters!” var day; day = document.getElementById('txtDay').value; if (day == "Sunday") { alert("No need to pay the meter!"); } else alert("Better get some quarters!");

Can your user spell? A top-notch programmer will go out of their way to make sure that their program will work regardless of the crazy kinds of things that a user might try to do. This can occupy lots of time and code, but the idea is important. For example, what if in our parking-meter example, a user types ‘sunday’ (no capital)? Answer: the conditional would evaluate to false since “sunday” does NOT equal “Sunday”. var day = "sunday"; // Note lower case ‘s’ if (day == "Sunday") // Will be FALSE

Anticipating different input Let’s re-write our conditional so that if the user enters “sunday” OR “Sunday” the conditional will hold true regardless. if (day == "Sunday" OR day == "sunday") … The word “OR” is not part of JavaScript. However, there IS a way of representing it: || The | (usually found above enter on your keyboard) is called the “pipe” character. Putting two pipes together (no space) is known as a “logical or”. When you have two or more comparisons separated by the “logical or” || , what has to happen for the conditional as a whole to evaluate to true? Answer: If *ANY* of the comparisons is true, the entire conditional will evaluate to true. if (day == "Sunday" || day == "sunday") If ANY of the above comparisons is true, the entire conditional will be true Incidentally, you don’t ned all the extra spaces. I just put them in there for teaching purposes. if (day == "Sunday" || day == "sunday" || day== "Domingo" || day== "Vendredi" || day== "Sondag") Again, if ANY of the above comparisons is true, the entire conditional will evaluate as true.

Slightly improved version: If the day is a Sunday, tell them they don’t have to feed the meter. If it is not Sunday, say: “Better get some quarters!” var day; day = document.getElementById('txtDay').value; if (day == "Sunday" || day== "sunday" ) { alert("No need to pay the meter!"); } else alert("Better get some quarters!");

Logical “AND” ( && ) Suppose you wanted to write a statement where two (or more) conditions must ALL be met for the entire conditional to be true. For example, suppose you wanted to see if a user was registered to vote. You might want to ask: Is the user 18 or older AND are they registered to vote? In other words, both conditions have to be true. The syntax follows: if ( age >= 18 && registered=="yes") { alert("You may vote") } else alert("You may not vote"); You can have as many conditions as you like inside the main conditional. When separated by logical AND ( && ), the rule is that ALL conditions must be true for the whole conditional to evaluate to true.

Logical “AND” ( && ) You can have as many conditions as you like inside the main conditional. When multiple conditions are separated by ‘&&’, all of them must be true for the whole expression to be true. if ( age >= 18 && registered=="yes" && notFelon=="yes") { alert("You may vote") } else alert("You may not vote");

Using braces for ‘if’ statements: If your ‘if’ statement has only one line of code, the braces are optional. if (condition) { statement 1 statement 2 } statement In general, I recommend that you ALWAYS use braces – even in those cases where your block is only one statement long. Only once you get very comfortable, should you use this shortcut. In fact, the only reason I’m showing it to you here is in case you see it elsewhere. For this course, you should always use braces. more than one statement – braces are required one statement only – braces are optional 14

File: vote_check.htm