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)