Javascript Conditionals.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Conditional Statements If these were easy then everyone would them!
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions In Python
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Selection in C.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
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
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Logic and Control. 4-2 Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) { document.write(“ you are a fifteen”);
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
22/11/ Selection If selection construct.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
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.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Making Decisions.
JavaScript.
Chapter 4: Making Decisions.
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Conditional Statements
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
If selection construct
Logical Operations In Matlab.
Visual Basic – Decision Statements
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Microsoft Visual Basic 2005: Reloaded Second Edition
JavaScript Part 2.
Topics The if Statement The if-else Statement Comparing Strings
M150: Data, Computing and Information
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Javascript Conditionals

The if Statement Concept: The if statement is used to create a decision structure, It allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is true.

Decision structure Decision (selection) structure: Diamond symbol represents a true/false condition Single alternative decision structure provides only one alternative path of execution.

Decision structure Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) document.write(“ you are a fifteen”);

Boolean Expressions and Relational Operators A relational operator (aka comparison operator) determines whether a specific relationship exists between two values. Table 4-1 Relational operators

Boolean Expressions and Relational Operators Examples: if (age > 90)… if (size < 10)… if (sum <= 50)… if (theNumber == 100)… if (x != 100)…

The if Statement - Blocks Examples: if (age > 90) { document.write (“you are a bit old”); } if (size< 10) document.write (“you are just the right size”); if (total >= 0) total += 50; if (name == “jasmine”) document.write (“nice name!”);

Try this! Write an if statement that assigns 0 to variable x if variable y is equal to 20 Write an if statement that assigns 0.2 to variable commission if variable sales is greater than or equal to 1000 Write an if statement that assigns “no match” to variable answer if variable name is not equal to “mary”

The if-else Statement Concept: An if-else statement will execute one block of statements if its condition is true, or another block if its condition is false.

dual alternative decision structure A dual alternative decision structure provides two possible paths of execution – one path is taken if a condition is true, the other path is taken if the condition is false.

The if-else Statement if (condition) { statement etc. } else

The if-else Statement if (age >= 20) { document.write(“you are an adult”); } else document.write(“you are NOT an adult”); if (salary <= 10) document.write(“you need a raise”); document.write(“you make a fine salary”);

Comparing Strings Concept: You can compare strings. This allows you to create decision structures that test the value of a string. If (password == “blahblah”) { document.write(“that is the correct password”); } else

Try This Purpose: Get a number from the user which is greater than 100 Prompt the user to enter a number that is greater than 100 If the number entered is greater than 100 – tell user it is a valid number If it less than 100 tell user it is invalid

Comparing Strings Other String Comparisons Figure 4-10 Character codes for the strings 'Mary' and 'Mark' Figure 4-11 Comparing each character in a string

Nested Decision Structures Concept: To test more than one condition, a decision structure can be nested inside another decision structure.

Nested Decision Structures

Nested Decision Structures if (condition_1) { statement etc. } else if (condition_2){ else{ The if-else if - else Statement

Nested Decision Structures if (score < 60) { document.write(‘Your grade is F.’); } else if (score < 70) { document.write( ‘Your grade is D.’); else if score < 80: document.write( ‘Your grade is C.’); else if score < 90 document.write( ‘Your grade is B.’); else { document.write( ‘Your grade is A.’);

Built in numeric functions isNaN( ) – determines whether a number is legal Returns True (if not a number) or False (if it is a number) NaN is an abbreviation for: Not a Number Examples of nonNumbers: A string A number divided by 0 alert(isNaN(4)) will return false alert(isNaN(“four”)) will return true

Try This Purpose: Get a number from the user which is numeric and greater than 100 Prompt the user to enter a number that is greater than 100 Check to see if the data entered is a number – if not tell the user that data is not a number If the number entered was a number then check If the number entered is greater than 100 – tell user it is a valid number If it less than 100 tell user it is not a valid number Hint: use if, else if

Logical Operators Logical operators are used to determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators: Operator Description Example && and (x < 10 && y > 1) is true || or (x==5 || y==5) is false ! not !(x==y) is true

Compound Conditions Many times you will need to check multiple conditions within the same statement Logical Operators allow you to check multiple conditions: Examples: if ((x > 100) && (x < 1000)) … if (age < 13) || (age > 65) …

Try This Purpose: Get a number from the user which is numeric and greater than 100 and less than 500 Prompt the user to enter a number that is greater than 100 Check to see if the data entered is a number – if not tell the user that data is not a number If the number entered was a number then check If the number entered is greater than 100 and less than 500 – tell user it is a valid number If it less than 100 or greater than 500, tell user it is not in range Hint: use if, else if

HTML DOM innerHTML Property The innerHTML property sets or returns the inner HTML of an element. We will use it to set error messages on fields Example: Styling error message #Nerror {color:red;} The input field with place for error message <label><input type="text" id=“num1"><span id=“Nerror"></span> Setting the innerHTML of the span element to conatin error message document.getElementById(" Nerror ").innerHTML = "Must be numeric";