Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010."— Presentation transcript:

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

2 Selection Statements if Statements switch Statements Conditional Operators

3 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); }

4 Note

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

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

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

8 Multiple Alternative if Statements

9 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

10 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

11 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

12 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

13 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

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

15 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.

16 TIP

17 Note

18 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.

19 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 }

20 switch Statement Flow Chart

21 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"); }

22 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.

23 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.

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

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

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

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

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

29 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

30 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":

31 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':

32 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

33 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

34 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

35 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

36 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

37 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”);

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

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


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

Similar presentations


Ads by Google