CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.

Slides:



Advertisements
Similar presentations
CSC 121 Computers and Scientific Thinking Fall Conditional Execution.
Advertisements

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.
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Operators and Statements Assignment Statements Mathematical Operators Conditional Logic if and switch statements – Ask a question (test a condition) –
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
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.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
JavaScript, Third Edition
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Introduction to C Programming
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CIS101 Introduction to Computing Week 12 Spring 2004.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Basic Of Computer Science
Flow of Control Part 1: Selection
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
MULTIPLE ALTERNATIVES IF… THEN… ELSEIF SELECT CASE.
CSD 340 (Blum)1 Switch and While. CSD 340 (Blum)2 From if to switch.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
CSD 340 (Blum)1 Arrays See Beginning JavaScript pp
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
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.
Chapter 3 Boolean Expressions Section 3.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 Type Called bool  Bool has only two possible values: True and False.
Discussion 4 eecs 183 Hannah Westra.
Loop Lab CSD 340 (Blum).
Python: Control Structures
Debugging and Random Numbers
IF statements.
The Selection Structure
Selection Statements by Ahmet Sacan
Javascript Conditionals.
Conditions and Ifs BIS1523 – Lecture 8.
JavaScript What is JavaScript? What can JavaScript do?
Logical Operations In Matlab.
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Lecture 6: Conditionals AP Computer Science Principles
JavaScript What is JavaScript? What can JavaScript do?
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Compound Conditionals
Types, Truth, and Expressions (Part 2)
Presentation transcript:

CSD 340 (Blum)1 Ifs

CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.

CSD 340 (Blum)3

4 First part of code

CSD 340 (Blum)5 Second part of code

CSD 340 (Blum)6 The if structure //change fore color if user has selected black background if(back == "black") { fore="yellow"; } After the keyword “if” comes a condition (a.k.a. a proposition) – something that is either true or false, followed by a statement or possibly a block of statements that are to be executed should the condition be true and not executed otherwise. (Page 66 Beginning JavaScript, Paul Wilton)

CSD 340 (Blum)7 Are you asking me or are you telling me? The code back = “black” is an assignment – it tells the variable back to take on the value “black” The code back == “black” is a Boolean expression – it asks whether the variable back is equal to “black” which may be true or false.

CSD 340 (Blum)8 Accidentally (on purpose) replacing == with = What happens if we have an assignment where we want a condition?

CSD 340 (Blum)9 The variable back is assigned the value “black.” Then the expression evaluates as true meaning the it’s true that the variable back was successfully assigned the value “black.” So then the statements inside the if block are executed, and the fore color is changed to yellow. This is how we ended up with a black back color and yellow fore color even though the user seems to have requested an orange back color.

CSD 340 (Blum)10 A problem: Since “Black” is not equal to “black” the fore color is not changed. Hence the back color and fore color end up to be the same and the message cannot be read.

CSD 340 (Blum)11 Modified code produces same result whether user enters “black” or “Black”

CSD 340 (Blum)12

CSD 340 (Blum)13 The Boolean Operator OR if(back == "black" || back=="Black") { fore="yellow"; } The two vertical lines (above the backslash on the keyboard) denote the Boolean OR operator. The if block is executed if either back equals “black” is true or if back equals “Black” (Page 74 Beginning JavaScript, Paul Wilton)

CSD 340 (Blum)14 The Boolean OR Truth Table ABA OR B False True FalseTrue

CSD 340 (Blum)15

CSD 340 (Blum)16 A better way to handle this particular problem: the toUpperCase function

CSD 340 (Blum)17

CSD 340 (Blum)18 A capital idea if(back.toUpperCase() == "BLACK") { fore="yellow"; } The pre-defined method toUpperCase() belongs to the String class and returns a string with any lowercase letters turned into the corresponding uppercase letters. –Note that it does not change to variable back to all capitals letters but makes a temporary copy with all capital letters in this case which we use for purposes of comparison. (Page 122 Beginning JavaScript, Paul Wilton)

CSD 340 (Blum)19 Allowing user to respond in another format

CSD 340 (Blum)20 Return to OR if(back.toUpperCase() == "BLACK" || back == "#000000") { fore="yellow"; } Using the OR (||) operator again allows the user to enter the color in the hexadecimal format or in name format. Note that on each side of the OR is a complete condition and not a set of possible values. Do not use the following if(back.toUpperCase() == "BLACK" || "#000000") //WRONG!!!!

CSD 340 (Blum)21 Incorrect ORing (the condition is always true)

CSD 340 (Blum)22 References Beginning JavaScript, Paul Wilton