03slide_2007 - Chapter 3 Control Statements EE3206/EE5805...

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: Chapter 3 Control Statements EE3206/EE5805 Java Programming & Application EE3206/EE5805 1 Objectives To declare boolean type and write Boolean expressions. To To distinguish between conditional and unconditional && and || To operators. To use Boolean expressions to control selection statements. To To implement selection control using if and if-else statements. To To implement selection control using switch statements. To To write expressions using the conditional operator . To To know the rules governing operand evaluation order, To operator precedence, and operator associativity. EE3206/EE5805 Java Programming & Application EE3206/EE5805 2 The boolean Type and Operators boolean boolean is a data type that has either true or false boolean value Often in a Often values and program you need to compare two store the result as a boolean Java provides six comparison operators (also Java known as relational operators) that can be used to compare two values. boolean b = (1 > 2); boolean EE3206/EE5805 Java Programming & Application EE3206/EE5805 3 Comparison and Boolean Operators Comparsion Operator < <= > >= == != Boolean Operator ! && || ^ Name less than less than or equal to greater than greater than or equal to equal to not equal to Name not and or exclusive or EE3206/EE5805 Java Programming & Application EE3206/EE5805 4 Truth Table for Boolean Operator p true false p1 false false true true !p false true p2 false true false true p1 && p2 false false false true Example !(1 > 2) is true, because (1 > 2) is false. !(1 > 0) is false, because (1 > 0) is true. Example (3 > 2) && (5 >= 5) is true, because (3 > 2) and (5 >= 5) are both true. (3 > 2) && (5 > 5) is false, because (5 > 5) is false. p1 false false true true p1 false false true true p2 false true false true p2 false true false true p1 || p2 false true true true p1 ^ p2 false true true Example (2 > 3) || (5 > 5) is false, because (2 > 3) and (5 > 5) are both false. (3 > 2) || (5 > 5) is true, because (3 > 2) is true. Example (2 > 3) ^ (5 > 1) is true, because (2 > 3) is false and (5 > 1) is true. (3 > 2) ^ (5 > 1) is false, because both (3 > 2) and (5 > 1) are true. 5 EE3206/EE5805 Java Programming & Application EE3206/EE5805 false Example: Determining Leap Year? This program first prompts the user to enter a year as an int value and checks if it is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400. It’s logical expression is as follow: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) LeapYear EE3206/EE5805 Java Programming & Application EE3206/EE5805 6 The & and | Operators & and | are same as && and || except the latter are and conditional (shortcut) Conditional Operators: &&, || Conditional Unconditional Operators: &, | Unconditional • Evaluate left side first and try to early terminate • Always evaluate both expressions If x is 1, what is x after this expression? (x > 1) & (x++ < 10) If x is 1, what is x after this expression? (x > 1) && (x++ < 10) How about (1 == x) | (10 > x++)? (1 == x) || (10 > x++)? EE3206/EE5805 Java Programming & Application EE3206/EE5805 7 Selection Statements if Statements if switch Statements switch Conditional Operators Conditional EE3206/EE5805 Java Programming & Application EE3206/EE5805 8 Simple if Statements Boolean Expression false (radius >= 0) false if (booleanExpression) { if statement(s); } true Statement(s) true area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area); (A) (B) if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } EE3206/EE5805 Java Programming & Application EE3206/EE5805 9 The if...else Statement if...else if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } true Boolean Expression false Statement(s) for the true case Statement(s) for the false case EE3206/EE5805 Java Programming & Application EE3206/EE5805 10 Multiple Alternative if Statements Case 1: completely specify if… else if… else if… … … … else Case 2: incompletely specify if… else if… else if… … … 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'; EE3206/EE5805 Java Programming & Application EE3206/EE5805 11 Caution Braces can be omitted if the block contains a single Braces statement. (but not recommended) Adding a semicolon at the end of an if clause is a Adding common mistake. if (expression); {……} The else clause matches the most recent if clause in The the same block. • Confused by improper indentation EE3206/EE5805 Java Programming & Application EE3206/EE5805 12 Example: Guessing Birth Date The program can guess your birth date. Run to see how it works. GuessBirthDate EE3206/EE5805 Java Programming & Application EE3206/EE5805 13 switch Statement Rules The value of case cannot be a The variable. It can only be a literal. If the break statement is not If present, the next case statement will be executed. (drop through) The default case, which is The optional, can be used to perform actions when none of the specified cases matches the switch-expression. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; … case valueN: statement(s)N; break; default: statement(s)-default; } EE3206/EE5805 Java Programming & Application EE3206/EE5805 14 switch Statement Flow Chart status is 0 Compute tax for single filers status is 1 Compute tax for married file jointly status is 2 Compute tax for married file separatly break break break status is 3 Compute tax for head of household break default Default actions Next Statement EE3206/EE5805 Java Programming & Application EE3206/EE5805 15 Examples of switch If ch = ‘b’, what is the output? switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } If ch = ‘a’, what is the value of ch after execution? switch case case case } (ch) 'a': 'b': 'c': { ch++; ch++; ch++; EE3206/EE5805 Java Programming & Application EE3206/EE5805 16 Conditional Operator ? : (booleanExpression) ? expression1 : expression2 The two code sections below are equivalent. if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); System.out.println( (num % 2 == 0)? num + “is even” : num + “is odd”); EE3206/EE5805 Java Programming & Application EE3206/EE5805 17 Operator Precedence var++, var-var +, - (Unary plus and minus), ++var,--var +, (type) Casting (type) ! (Not) (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction) <, <=, >, >= (Comparison) ==, !=; (Equality) == & (Unconditional AND) (Unconditional ^ (Exclusive OR) (Exclusive | (Unconditional OR) (Unconditional && (Conditional AND) Short-circuit AND && || (Conditional OR) Short-circuit OR || =, +=, -=, *=, /=, %= (Assignment operator) HIGH LOW EE3206/EE5805 Java Programming & Application EE3206/EE5805 18 Operator Precedence and Associativity If operators with the same precedence are next to If each other, their associativity determines the order of evaluation. Left-associative Left • All binary operators except assignment operators • a – b + c – d is equivalent to ((a – b) + c) – d Right-associative Right • Assignment operator • Unary operator • a = b += c = 1 is equivalent to a = (b += (c = 1)) EE3206/EE5805 Java Programming & Application EE3206/EE5805 19 Operand Evaluation Order In Java, the left-hand operand of a binary In operator is evaluated before any part of the right-hand operand is evaluated. int x = 1; int y = x + (++x); int x = 1; int y = (++x) + x; // y becomes 3 // y becomes 4 EE3206/EE5805 Java Programming & Application EE3206/EE5805 20 Rule of Evaluating an Expression Applying the rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is Applying evaluated as follows: 3 + 4 * 4 > 5 * (4 + 3) - 1 3 + 16 > 5 * (4 + 3) - 1 19 > 5 * (4 + 3) - 1 19 > 5 * 7 - 1 19 > 35 – 1 (1) 4 * 4 is the first subexpression that can be evaluated from left. (2) 3 + 16 is evaluated now. (3) 4 + 3 is now the leftmost subexpression that should be evaluated. (4) 5 * 7 is evaluated now. (5) 35 – 1 is evaluated now. 19 > 34 (6) 19 > 34 is evaluated now. false EE3206/EE5805 Java Programming & Application EE3206/EE5805 21 More Examples EE3206/EE5805 Java Programming & Application EE3206/EE5805 22 Example: A Simple Math Learning Tool This example creates a program to let a first grader practice additions. The program randomly generates two single-digit integers number1 and number2 and displays a question such as “What is 7 + 9?” to the student, as shown below. After the student types the answer in the input dialog box, the program displays a message dialog box to indicate whether the answer is true or false. LearnAddition EE3206/EE5805 Java Programming & Application EE3206/EE5805 23 Example: 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. EE3206/EE5805 Java Programming & Application EE3206/EE5805 24 Example: 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 } ComputerTax EE3206/EE5805 Java Programming & Application EE3206/EE5805 25 Example: An Improved Math Learning Tool This example creates a program to teach a first grade child how to learn subtractions. The program randomly generates two single-digit integers number1 and number2 with number1 > number2 and displays a question such as “What is 9 – 2?” to the number2 student, as shown in the figure. After the student types the answer in the input dialog box, the program displays a message dialog box to indicate whether the answer is correct, as shown in figure. SubtractionTutor EE3206/EE5805 Java Programming & Application EE3206/EE5805 26 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online