Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Visual C++ Programming: Concepts and Projects
Running JavaScript Chapter 18.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Java Programming, Second Edition Chapter Five Input and Selection.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Using Client-Side Scripts to Enhance Web Applications 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Copyright 2003 Scott/Jones Publishing Making Decisions.
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Control statements Mostafa Abdallah
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Popup Boxes.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 6 Controlling Program Flow with Looping Structures.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
INTERMEDIATE PROGRAMMING USING JAVA
Chapter 4: Making Decisions.
Operator Precedence Operators Precedence Parentheses () unary
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,
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Javascript Conditionals.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
JavaScript: Control Statements I
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Selection Statements.
Relational, Logical, and Equality Operators
Program Flow.
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC215 Lecture Control Flow.
Controlling Program Flow
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved

JavaScript Booleans What is a Boolean? A Boolean value equates to either true or false. Examples: true; // equates to true false; // equates to false TRUE; // error (incorrect case) True; // error (incorrect capitalization) 4 == 4; // true (equality) 4 == "4"; // true (equality) 4 === 4; // true (identity) 4 === "4"; // false (not identity) 2 booleans.html

What is false? Examples: Boolean( 0 ); // false Boolean( -0 ); // false Boolean( "" ); // (empty string) Boolean( null ); // false Boolean( ! true ); // false 3 false.html

What is also false? Examples: var x; Boolean( x ); // (undefined) Boolean( NaN ); // false Boolean( 0 / 0 ); // (NaN) Boolean( 4 / "Fred" ); // (NaN) 4 falseAlso.html

What is true? Examples: 1; // true -1; // true 3 < 4; // true 4 <= 4; // true 3 != 4; // true (not equality) "4" !== 4; // true (not identity) !(3 > 4); // true 5 true.html

What is also true? Examples: Boolean( ! false ); // true Boolean( "this is a string" ); Boolean( 1 / 0 ); // (Infinity) Boolean( -1 / 0 ); // (-Infinity) Boolean( Infinity ); // true Boolean( -Infinity ); // true 6 trueAlso.html

Dialog: confirm() Description: The confirm() dialog displays “OK” and “Cancel” buttons. 7

Dialog: confirm() Description: The confirm() dialog returns a true if the user presses “OK” or false if the user presses “Cancel”. // open confirm dialog var answer = window.confirm( "OK for true.\n" + "Cancel for false."); console.log( answer ); 8 confirm.html

“if” Statement Flow Chart Description: The “if” statement has one branch. If the condition is true, the block of code is executed. If the condition is false, the block is skipped. 9 IF Code Block truefalse “if” branch “else” branch

What is an “if” statement? Description: A statement that allows you to conditionally execute a block of code. If the condition is true, the code block is executed. Syntax: if ( condition ) { // true // code statement; } 10

Conditional Statement: if // open confirm dialog var answer = window.confirm( "OK for true.\n" + "Cancel for false."); if ( answer == true ) { console.log( "You pressed OK" ); } 11 confirmIf.html Would this also work? if ( answer ) Would this also work? if ( answer )

One Line “if” Statement Discussion: The “if” condition does not require the curly braces “{}” if there is only a single statement in the code block. Example: var isLogin = true; if ( isLogin ) console.log( "Success" ); 12 ifOneLine.html

Discussion: The “if” condition does not require the curly braces “{}” when there is only one statement in the code block, however best practices or a style guideline may encourage its use. Example: var username = "admin"; if ( username == "admin" ) console.log( "Access granted" ); Single Statement “if” Condition 13 ifNoCurly.html

“if else” Statement Flow Chart Description: The “if” statement has one branch. If the condition is true, the block of code is executed. If the condition is false, the block is skipped. 14 IF Code Block truefalse Code Block “if” branch “else” branch

What is an “else” statement? Description: A statement that provides a fall back branch in case the “if” statement is false. Syntax: if ( condition ) { // true // code statement; } else { // if ( ! condition ) // code statement; } 15

Conditional Statements: if & else // open confirm dialog var answer = window.confirm( "OK for true.\nCancel for false."); if ( answer == true ) { console.log( "You pressed OK" ); } else { // if ( answer == false ) console.log( "You pressed Cancel" ); } 16 confirmElse.html

Code Block true Nested “else” Flow Chart Description: In order to handle more than two branches, an “if” statement can be nested inside of an “else” statement. 17 IF Code Block truefalse “if” branch “else” branch IF Code Block truefalse “else” branch IF Code Block false “else” branch true IF false “else” branch

“else if” Flow Chart Description: In order to handle more than two branches, an “if” statement can be nested inside of an “else” statement. 18 IF Code Block “if” true “if” branch ELSE IF ELSE IF Code Block “else if” true “if” false “else if” branch ELSE Code Block “else if” false “else” branch Code Block ELSE IF ELSE IF “else if” true “else if” branch “else if” false when “if” and all “else if” branches are false

What is an “else if” statement? Description: A statement that provides multiple branching in an “if” statement. Syntax: if ( condition ) { // true // code statement; } else if ( condition ) { // true // code statement; } else { // if all conditions false // code statement; } 19

Dialog: prompt() Description: The confirm() dialog displays a text box, “OK” and “Cancel” buttons. 20

Statements: if, else if, & else // open text prompt dialog var answer = window.prompt("Enter YES or NO"); if ( answer == "YES" ) { console.log( "You entered YES" ); } else if ( answer == "NO" ) { console.log( "You entered NO" ); } else { console.log( "Didn't follow directions!" ); } 21 promptElseIf.html

Logical Operators Defined && (aka Logical “AND”) Defined: “AND” is true only if both operands are true || (aka Logical “OR”) Defined: “OR” is true only if at least one operand is true ! (aka Logical “NOT”) Defined: “NOT” is true only if operand is false (invert Boolean) 22

Logical Operator Examples ! true; // false ! false; // true true && true; // true true && false; // false true || true; // true true || false; // true 23 logicOp.html

Logical Operator Precedence Discussion: What happens when multiple logical operators are used together? Examples: // && is higher precedence // and evaluated first true || true && false; // true true || ( true && false ); // true true || ( false ); // true true; 24 logicOpMulti.html

Logical Equivalence Discussion: It is often useful to know what the equivalence of a logical “OR” and a logical “AND”. Examples: // ! highest precedence !( true || false ); // false (! true ) && (! false ); // false !( true && false ); // true (! true ) || (! false ); // true 25 logicEquiv.html

var value = 4; if ( value >= 1 ) { // min range if ( value <= 10 ) { // max range console.log( "Between 1 & 10" ); } else { console.log( "Out of range" ); } Nesting “if” Statements 26 nestedIf.html

var value = 4; if ( ( value >= 1 ) && // min range ( value <= 10 ) ) { // max console.log( "Between 1 and 10" ); } else { console.log( "Out of range" ); } Conditional: “if” and “&&” 27 ifAnd.html

Conditional: “if” and “||” // open text prompt dialog var answer = window.prompt("Type YES or NO"); if ( ( answer == "yes" ) || // lowercase ( answer == "YES" ) ) { // uppercase console.log( "You typed YES" ); } else if ( ( answer == "no" ) || // lowercase ( answer == "NO" ) ) { // uppercase console.log( "You typed NO" ); } else { console.log( "Didn't follow directions!" ); } 28 ifOr.html

“if” and Regular Expressions // open text prompt dialog var answer = window.prompt("Type YES or NO"); var regexYes = /^y$|yes/i; // "y" or "yes" var regexNo = /^n$|no/i; // "n" or "no" if ( regexYes.test( answer ) ) { console.log( "You typed YES" ); } else if ( regexNo.test( answer ) ) { console.log( "You typed NO" ); } else { console.log( "Didn't follow directions!" ); } 29 ifRegExp.html

Conditional Statement: “switch” var answer = window.prompt("Type YES or NO"); switch ( answer ) { case 'YES' : console.log( "You typed \"YES\"" ); break; // exit case case 'NO' : console.log( "You typed \"NO\"" ); break; // exit case default : // else no other matches console.log( "You rebel!" ); break; // exit case } 30 switch.html

Switch with Fall-Through var answer = window.prompt("Type YES or NO"); switch ( answer ) { case 'yes' : // no break (fall-thru) case 'YES' : console.log( "You typed \"YES\"" ); break; // exit case case 'no' : // no break (fall-thru) case 'NO' : console.log( "You typed \"NO\"" ); break; // exit case default : // else no other matches console.log( "You rebel!" ); break; // exit case } 31 switchFallThru.html