Chapter III Part I

Chapter III Part I - Introduction to Control Statements...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Control Statements IT108 George Mason University Selection Statements if Statements switch Statements Conditional Operators Simple if Statements if (radius >= 0) { area = radius * radius * PI; System.out.println("The area" + " for the circle of radius " + radius + " is " + area); } if (booleanExpression) { statement(s); } Boolean Expression false (radius >= 0) false true true Statement(s) (A) area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area); (B) Note Outer parentheses required if ((i > 0) && (i < 10)) { System.out.println("i is an " + + "integer between 0 and 10"); } (a) Braces can be omitted if the block contains a single statement Equivalent if ((i > 0) && (i < 10)) System.out.println("i is an " + + "integer between 0 and 10"); (b) Caution Common Logic Error if (radius >= 0); Wrong { area = radius*radius*PI; System.out.println( "The area for the circle of radius " + radius + " is " + area); } The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } true Statement(s) for the true case Boolean Expression false Statement(s) for the false case if...else Example if (radius >= 0) { area = radius * radius * 3.14159; System.out.println(“The area for the “ + “circle of radius “ + radius + “ is “ + area); } else { System.out.println(“Negative input”); } Multiple Alternative if Statements 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'; Equivalent 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'; Trace if-else statement Suppose score is 70.0 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'; The condition is false Trace if-else statement Suppose score is 70.0 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'; The condition is false Trace if-else statement Suppose score is 70.0 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'; The condition is true Trace if-else statement Suppose score is 70.0 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'; grade is C Trace if-else statement Suppose score is 70.0 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'; Exit the if statement Note The else clause matches the most recent if clause in the same block. int i = 1; int j = 2; int k = 3; Equivalent if (i > j) if (i > k) System.out.println("A"); else System.out.println("B"); (a) int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B"); (b) 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: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B"); This statement prints B. TIP if (number % 2 == 0) even = true; else even = false; (a) Equivalent boolean even = number % 2 == 0; (b) Note if (even == true) System.out.println( "It is even."); (a) Equivalent if (even) System.out.println( "It is even."); (b) 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 status is 0 Compute tax for single filers break Compute tax for married file jointly break Compute tax for married file separatly break Compute tax for head of household break status is 1 status is 2 status is 3 default Default actions Next Statement 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: System.out.println("Errors: invalid status"); System.exit(0); } switch Statement Rules The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. The value1, ..., and valueN must have the same data type as the value of the switchexpression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. Note that value1, ..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x. 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; } 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. The default case, which is optional, can be used to perform actions when none of the specified cases matches the switchexpression. 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 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 Suppose ch is 'a': switch case case case } (ch) 'a': 'b': 'c': { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement ch is 'a': switch case case case } (ch) 'a': 'b': 'c': { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement Execute this line switch case case case } (ch) 'a': 'b': 'c': { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement Execute this line switch case case case } (ch) 'a': 'b': 'c': { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement Execute this line switch case case case } (ch) 'a': 'b': 'c': { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement Execute next statement switch case case case } (ch) 'a': 'b': 'c': Next statement; { System.out.println(ch); System.out.println(ch); System.out.println(ch); Trace switch statement Suppose ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } Trace switch statement ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } Trace switch statement Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } Trace switch statement Execute next statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } Next statement; Example import javax.swing.JOptionPane; public class Average { public static void main (String [ ] args) { int exam1, exam2; int average; char letterGrade; exam1 = Integer.parseInt(JOptionPane.showInputDialog("Enter exam 1 grade (0-100)")); exam2 = Integer.parseInt(JOptionPane.showInputDialog("Enter exam 2 grade (0-100)")); average = (exam1 + exam2) /2; 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'; } } JOptionPane.showMessageDialog(null, "Average: " + average + "\nGrade: " + letterGrade); Example Payroll Application Using the number of hours worked, entered by the user, and an hourly rate of $8 an hour… calculate Gross Pay. Note: overtime is paid at a rate of time and a half. ...
View Full Document

This note was uploaded on 01/26/2012 for the course IT 108, 103, taught by Professor Bruno during the Spring '11 term at George Mason.

Ask a homework question - tutors are online