Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.

Slides:



Advertisements
Similar presentations
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
Control Structures Corresponds with Chapters 3 and 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 05 Title: Decision/Selection Control Structures Reference: MalikFarrell, chap 1, Liang Ch 3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Information  HW1 now available  Group Project (Find your group members)  Start thinking about group projects.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chapter 3 Selection Statements §3.1 The Boolean Type and Operators §3.2 The if-else Statements §3.3 Case Studies §3.4 Logical Operators §3.5 Switch Statements.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Introduction to Control Statements IT108 George Mason University.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Lecture 4: Introduction to Control Flow Statements and Reading User Input Michael Hsu CSULA.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Group Project (Find your group members)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Selections Java.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Presentation transcript:

Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010

Selection Statements if Statements switch Statements Conditional Operators

Simple i f Statements if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; document.write("The area" + " for the circle of radius " + radius + " is " + area); }

Note

Caution Common Logic Error if (radius >= 0); { area = radius*radius*PI; document.write( "The area for the circle of radius " + radius + " is " + area); } Wrong

The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

if...else Example if (radius >= 0) { area = radius * radius * ; document.write(“The area for the “ + “circle of radius “ + radius + “ is “ + area); } else { document.write(“Negative input”); }

Multiple Alternative if Statements

Trace if-else statement if (score >= 90.0) grade = "A"; else if (score >= 80.0) grade = "B"; else if (score >= 70.0) grade = "C"; else if (score >= 60.0) grade = "D"; else grade = "F"; Suppose score is 70.0The condition is false

Trace if-else statement if (score >= 90.0) grade = "A"; else if (score >= 80.0) grade = "B"; else if (score >= 70.0) grade = "C"; else if (score >= 60.0) grade = "D"; else grade = "F"; Suppose score is 70.0The condition is false

Trace if-else statement if (score >= 90.0) grade = "A"; else if (score >= 80.0) grade = "B"; else if (score >= 70.0) grade = "C"; else if (score >= 60.0) grade = "D"; else grade = "F"; Suppose score is 70.0The condition is true

Trace if-else statement if (score >= 90.0) grade = "A"; else if (score >= 80.0) grade = "B"; else if (score >= 70.0) grade = "C"; else if (score >= 60.0) grade = "D"; else grade = "F"; Suppose score is 70.0grade is C

Trace if-else statement if (score >= 90.0) grade = "A"; else if (score >= 80.0) grade = "B"; else if (score >= 70.0) grade = "C"; else if (score >= 60.0) grade = "D"; else grade = "F"; Suppose score is 70.0Exit the if statement

Note The else clause matches the most recent if clause in the same block.

Note, cont. Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: i = 1; j = 2; k = 3; if (i > j) { if (i > k) document.write("A"); } else document.write("B"); This statement prints B.

TIP

Note

Example 3.1 Computing Taxes The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. The tax rates for 2002 are shown in Table 3.1.

Example 3.1 Computing Taxes, cont. if (status == 0) { // Compute tax for single filers } else if (status == 1) { // Compute tax for married file jointly } else if (status == 2) { // Compute tax for married file separately } else if (status == 3) { // Compute tax for head of household } else { // Display wrong status }

switch Statement Flow Chart

switch Statements switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married file jointly; break; case 2: compute taxes for married file separately; break; case 3: compute taxes for head of household; break; default: document.write("Errors: invalid status"); }

switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The switch-expression must always be enclosed in parentheses.

switch Statement Rules The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The case statements are executed in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.

Trace switch statement switch (ch) { case "a": document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } Suppose ch is "a":

Trace switch statement switch (ch) { case 'a': document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } ch is "a":

Trace switch statement switch (ch) { case "a": document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } Execute this line

Trace switch statement switch (ch) { case "a": document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } Execute this line

Trace switch statement switch (ch) { case "a": document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } Execute this line

Trace switch statement switch (ch) { case "a": document.write(ch); case "b": document.write(ch); case "c": document.write(ch); } Next statement; Execute next statement

Trace switch statement switch (ch) { case "a": document.write(ch); break; case "b": document.write(ch); break ; case "c": document.write(ch); } Suppose ch is "a":

Trace switch statement switch (ch) { case 'a': document.write(ch); break; case "b": document.write(ch); break ; case "c": document.write(ch); } ch is 'a':

Trace switch statement switch (ch) { case "a": document.write(ch); break; case "b": document.write(ch); break ; case "c": document.write(ch); } Execute this line

Trace switch statement switch (ch) { case "a": document.write(ch); break; case "b": document.write(ch); break ; case "c": document.write(ch); } Execute this line

Trace switch statement switch (ch) { case 'a': docuent.write(ch); break; case 'b': document.write(ch); break ; case 'c': document.write(ch); } Next statement; Execute next statement

Example using Else If structure exam1 = parseFloat(prompt("Enter exam 1 grade (0-100)")); // convert input to number exam2 = parseFloat(prompt("Enter exam 2 grade (0-100)")); // convert input to number average = (exam1 + exam2) /2; // need to have converted to number, otherwise string concatenation // here if (average > 90) letterGrade = "A"; else if (average > 80) letterGrade = "B"; else if (average >70) letterGrade = "C"; else if (average >60) letterGrade = "D"; else letterGrade ="F"; document.write("Average: " + average + "Grade: " + letterGrade); } } Switch statement only tests for equality and thus one would have to do calculations to use a switch statement for the above problem

Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2 Ternary operator Binary operator Unary operator

Conditional Operator if (num % 2 == 0) document.write(num + “ is even”); else document.write(num + “ is odd”); document.write( (num % 2 == 0)? num + “ is even” : num + “ is odd”);

Conditional Operator, cont. (booleanExp) ? exp1 : exp2

39 Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)