Modern JavaScript Develop And Design

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
JavaScript, Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
UNIT II Decision Making And Branching Decision Making And Looping
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Comp 248 Introduction to Programming Chapter 3 – Flow of Control Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
LING 408/508: Programming for Linguists Lecture 11 October 5 th.
Decision Statements, Short- Circuit Evaluation, Errors.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Learning Javascript From Mr Saem
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Discussion 4 eecs 183 Hannah Westra.
>> Introduction to JavaScript
Java Programming Fifth Edition
Chapter 4: Making Decisions.
Flow of Control.
Chapter 3 Branching Statements
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Modern JavaScript Develop And Design
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Week#2 Day#1 Java Script Course
JavaScript: Control Statements I
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Chapter 8 JavaScript: Control Statements, Part 2
During the last lecture we had a discussion on Data Types, Variables & Operators
PHP.
University of Kurdistan
Chapter 6 Control Statements: Part 2
Logical Operations In Matlab.
Statement-Level Control Structures
Chapter 4: Control Structures I (Selection)
The <script> Tag
Controlling Program Flow
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Modern JavaScript Develop And Design Instructor’s Notes Chapter 5 – Using Control Structures Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman

Objectives Understand what JavaScript considers to be TRUE and FALSE Know how to write several types of conditionals Recognize the comparison operators Identify and prevent many common bugs related to conditionals

More Objectives Recognize the logical operators Perform string comparisons and searches Define loops Set HTML content using DOM manipulation

True and False These are FALSE: false an empty string NaN null an empty string NaN null undefined One must understand what JavaScript considers to be TRUEin order to use conditionals. These are FALSE, everything else is considered to be TRUE. What out for 0 and an empty string!

If Conditional if (condition) { // Execute these statements. } if is a branching statement. If the condition is TRUE, the statement or statements within the curly braces will be executed. if the condition is FALSE, the statements are not executed. Technically, JavaScript allows you to omit the curly braces if there is only one executed statement, but I almost always recommend you use them. Do indent the statements to indicate they are subservient to the IF.

True or False? var myVar; if (myVar) {… var myVar = 0; if (myVar) {… First two are FALSE; last one is TRUE.

Comparison Operators Operator Meaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to === Identical to !== Not identical to Be very careful with =, ==, and ===.

Bugs! Wrong Right var myVar; if (myVar = 2) {… var myVar; if (myVar !== undefined) {… The first two are bugs. The last one is the proper way to right the middle conditional. Must be an IDENTICAL comparison. var myVar = 0; if (myVar) {… var myVar = 0; if (myVar !== false) {…

Tip Perform an identical comparison when you want to confirm a variable has a value of undefined null false As opposed to FALSE-like value.

Comparing Numbers Be careful when checking equality of numbers with decimals.

Solutions Limit decimals Perform string comparisons Perform integer comparisons var n = 1 - .8; n = n.toFixed(1); var m = .3 - .1; m = m.toFixed(1); if (n == m) { // TRUE! Money is normally treated as integers, in cents.

Comparing NaN You cannot compare a number to NaN. NaN !== NaN Use isNaN(). Use isFinite() to also rule out infinite values.

Comparing Strings String comparisons are case-sensitive! Use toLowerCase() or toUpperCase() var s1 = 'cat'; var s2 = 'Cat'; if (s1 == s2) { // FALSE! var s1 = 'cat'; var s2 = 'Cat'; if (s1.toLowerCase() == s2.toLowerCase()) { // FALSE!

Confirming Substrings indexOf() returns 0 if the substring is first 0 is treated as FALSE var s1 = 'cat'; var s2 = 'catalog'; if (s2.indexOf(s1)) { // BUG! var s1 = 'cat'; var s2 = 'catalog'; if (s2.indexOf(s1) != -1) { // Right!

Logical Operators Operator Meaning && And || Or ! Not The OR is two of the PIPE characters together. Not negates anything.

Compound Conditions Condition True/False (0 < 5) && (5 < 10) (0 < 5) && (5 > 10) False (0 > 5) && (5 > 10) (0 < 5) || (5 < 10) (0 < 5) || (5 > 10) (0 > 5) || (5 > 10) A compound AND conditional is only true if all subconditions are TRUE. A compound OR conditional is true if any subcondtion is TRUE. Use parentheses for clarity Short circuit evaluation: JavaScript will stop evaluating a condition as soon as it knows the condition will be TRUE or FALSE.

If-Else Conditional if (condition) { // Execute these statements. // Execute these statements instead. } else is the default clause

If-Else If Conditional // Execute these statements. } else if (condition2) { // Execute these statements instead. } else and if are two separate words. With no else clause, there’s no default.

If-Else If-Else Conditional if (condition1) { // Execute these statements. } else if (condition2) { // Execute these statements instead. } else { // Now execute these. } else clause, which is optional, must always come last.

Switch Conditional switch (expression) { case value1: // Execute these statements. break; case value2: // Execute these statements instead. case value3: // Execute these statements now. default: // Default statements. } Switch is an alternative to if-else if. Must use break or the subsequent statements will continue to execute. default is optional. Performs an identity match.

Conditional Operator (condition) ? return_if_true : return_if_false; var even = ( (n % 2) === 0) ? true : false; Note that this operator returns one of two values; it does not execute statements as the other conditionals do. Can be used inline. Also called ternary or trinary in other languages.

typeof Operator Type typeof Value Undefined undefined Null object Boolean boolean Number number String string Array Object typeof is often a better way to confirm a value’s type. Just be careful with null, array, and object if (typeof myVar == 'number') {

The for Loop for (initial; condition; after) { // Execute these statements. } Two primary loops in JavaScript: for and while. The for loop is used to iterate a known number of times, such as once for each element in an array. The initial expression is always evaluated just once. The condition is checked each time. If TRUE, the statements are executed. The after expression is executed once for each loop iteration. The third clause, or something in the statements, should eventually make the condition be FALSE. for (var i = 1; i <= 10; i++) { // Use i. }

The while Loop while (condition) { // Statements. } do { do…while is always executed at least once.

Setting HTML Content textContent innerText innerHTML if (elem.textContent !== undefined) { // Use elem.textContent. } else { // Use elem.innerText. } Unrelated to control structures, but necessary information is the ability to set HTML content. textContent is the W3C standard and works on most browsers innerText is for IE