Conditional Statements with JavaScript

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
Conditional Statements Introduction to Computing Science and Programming I.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Class03 Introduction to Web Development (Hierarchy and the IDE) MIS 3501, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript: Conditionals contd.
BIT116: Scripting Lecture 05
FOP: While Loops.
Class03 Introduction to Web Development (Hierarchy and the IDE)
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Programming Web Pages with JavaScript
Whatcha doin'? Aims: To start using Python. To understand loops.
Learning to Program D is for Digital.
Introduction to Web Development (Part 2)
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
PHP: includes MIS 3501 Jeremy Shafer Department of MIS
Debugging and Random Numbers
Operator Precedence Operators Precedence Parentheses () unary
Retrieving information from forms
Javascript Conditionals.
JavaScript.
The structure of computer programs
Introduction to JavaScript
Introduction To Robot Decision Making
Lesson 2: Building Blocks of Programming
JavaScript conditional
A second look at JavaScript
Sessions and cookies (part 1)
T. Jumana Abu Shmais – AOU - Riyadh
Class07 PHP: loops MIS 3501 Jeremy Shafer Department of MIS
Introduction To Robot Decision Making
Sessions and cookies MIS 3501 Jeremy Shafer Department of MIS
MIS JavaScript and API Workshop (Part 2)
An Introduction to JavaScript
Programming Control Structures with JavaScript Part 2
Getting started with jQuery
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Loops and Arrays in JavaScript
JavaScript objects, functions, and events
JavaScript conditional
Introduction to JavaScript
Tutorial 10: Programming with javascript
An introduction to jQuery
Introducing JavaScript
Javascript Chapter 19 and 20 5/3/2019.
The Selection Structure
Programming Control Structures with JavaScript
An introduction to jQuery
If / Else Statements.
Web Programming and Design
Intro to Programming (in JavaScript)
Conditionals.
Retrieving information from forms
Presentation transcript:

Conditional Statements with JavaScript MIS 2402 Jeremy Shafer Department of MIS Fox School of Business Temple University

Let’s talk about… Sandwiches: How is programming like a sandwich? Some other food for thought: How do we assign a value to a JavaScript variable? When we say two things are equal, are we assigning or comparing those two things?

Agenda Using breakpoints in the Chrome Developer Tools A new data type Logical Expressions Putting logical expressions to work: If statements A couple remarks about variables

Breakpoints Setting one or more breakpoints in your code will allow you, the developer, to get a better understanding of how your code is working. Let’s see an example of this. (see rocket.zip)

A new data type Discuss: What were the two data types we introduced last class? Today we are going to introduce another data type: the Boolean value. Boolean means “true” or “false” and… it gets its name from a famous mathematician named George Boole. George Boole lived long before computers were invented. If only he could see where his ideas have taken us! In JavaScript we can explicitly assign a Boolean value like this: var some_name = true; var some_other_name = false;

The relational operators Usually we are more interested in determining true or false values by comparing two things. We use the following operators to compare values.

Conditional expressions This code illustrates some logical expressions. Logical expressions evaluate to true or false. What do each of these expressions evaluate to? Think quick! If == is a comparison operator, what is the assignment operator in JavaScript?

Another test The syntax of the global isNaN method Sometimes we need to test a variable and figure if it is a number or not. For that we use something called isNaN. The abbreviation isNaN means “is Not a Number”. The syntax of the global isNaN method isNaN() is a global method. The term “global” means it is available everywhere in your JavaScript code. Global methods are also sometimes called functions.

The logical operators OK, now we are getting fancy! It is often useful to consider two or more boolean expressions at the same time. For example: we might want to know if condition “A” and condition “B” are both true. To build such compound expressions, we use logical operators.

Conditional expressions with logical operatiors Expressions evaluate to true or false. Given: var testScore = 100; var age = 55; var limit = 60; What do each of these expressions evaluate to? So – what does this evaluate to: !(1 == 2) && 'monkey' == 'monkey' || 'elephant' == 'hippo'

Putting conditional expressions to work! The syntax of the if statement It’s a code block “sandwich”. The “{“ and “}” hold one or more lines of code.

An if statement with multiple else clauses

( See rocket_01.html through rocket_05.html ) Let’s give this a try… ( See rocket_01.html through rocket_05.html )

Variables don’t live for long… When the script in index.html runs, the variables x and y get briefly created, used, and destroyed. Those variable just don’t exist in file01.html and file02.html. If we try, the JavaScript will not run in file01 and file02 because x and y simply don’t exist in that context. The browser’s “brain” index.html file01.html file02.html var x = ‘Fred’; var y = 3.14; console.log(x); console.log(y); x and y are used and immediately discarded. Error! Error!

It would be nice if … It would be nice if there was a way to declare a variable in such a way that it stayed in the browser’s brain. Surprise! Such a thing exists! It’s called “Session Storage” and it allows you to store data in the browser’s memory for as long as the browser software is running. The sessionStorage object is built into the browser for you to use. So, what this object allows you to do is to specify the reserved word “sessionStorage” followed by a dot “.” followed by an identifier of your own choosing. It’s a pretty easy syntax! sessionStorage.myMadeUpName = "owl";

Let’s try sessionStorage x and y are now in the browser’s memory: The browser’s “brain” sessionStorage.x = ‘Fred’; sessionStorage.y = 3.14; index.html file01.html file02.html sessionStorage.x = ‘Fred’; sessionStorage.y = 3.14; console.log(sessionStorage.x ); console.log(sessionStorage.y); This runs Works! Works!

Historic note There are a variety of ways to achieve this effect of “remembering” data from page to page – but the Session Storage option is probably the easiest. This option came into existence as part of the HTML 5 specification. So, all major browsers now support the session storage object. So... You were born at the right time! Other/older approaches used something called “cookies” and were pretty complicated.

What does this have to do with “if” statements? Sometimes we want to check if a sessionStorage item exists before we try to use it. We use a conditional statement to check if the item is present. If “x” exists this will evaluate as true, and false if “x” doesn’t exist! Like this: if (sessionStorage.x){ console.log("I remembered what x is!"); console.log(sessionStorage.x); } else { console.log("I never heard of x. What are you talking about?"); }              See “remembering” and “forgetting” in brain.zip

Next time …