Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter Control Statements Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter Introduction to Computers, Programs, and Java Chapter Primitive Data Types and Operations Chapter Selection Statements Chapter Loops Chapter Methods .. in Chapter Recursion Chapter Arrays Chapter Algorithm Efficiency and Sorting Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. Objectives To declare boolean type and write Boolean expressions .. To distinguish between conditional and unconditional ampamp and operators ... To use Boolean expressions to control selection statements ... To implement selection control using if and nested if statements .. To implement selection control using switch statements .. To write expressions using the conditional operator . . To display formatted output using the System.out.printf method and to format strings using the String.format method .. To know the rules governing operand evaluation order, operator precedence, and operator associativity .. . Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. The boolean Type and Operators Often in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators also known as relational operators that can be used to compare two values. The result of the comparison is a Boolean value true or false. boolean b gt Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. Introduction to Java Programming. . Inc. All rights reserved.Comparison Operators Operator Name lt lt gt gt less than less than or equal to greater than greater than or equal to equal to not equal to Liang. c Pearson Education. Sixth Edition. Boolean Operators Operator Name ampamp not and or exclusive or Liang. . Inc. Sixth Edition. All rights reserved. c Pearson Education. Introduction to Java Programming. Liang. because gt is alse. Introduction to Java Programming. gt is alse.Truth Table for Operator p true false p false true xample gt is true. because gt is true. Sixth Edition. . All rights reserved. c Pearson Education. Inc. Truth Table for Operator ampamp p false false true true p false true false true p ampamp p false false false true xample gt is true. . All rights reserved. Sixth Edition. Liang. Introduction to Java Programming. gt gt is alse. Inc. because gt gt and gt are both true. c Pearson Education. because gt is alse. Truth Table for Operator p false false true true p false true false true p p false true true true xample gt gt is alse, because gt and gt are both alse. gt gt is true, because gt is true. Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. Truth Table for Operator p false false true true p false true false true p p false true true false xample gt gt is true, because gt is alse and gt is true. gt gt is alse, because both gt and gt are true. Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. Examples System.out.printlnquotIs quot num quot divisible by and quot num ampamp num System.out.printlnquotIs quot num quot divisible by or quot num num System.out.printlnquotIs quot num quot divisible by or , but not both quot num num Liang, Introduction to Java Programming, Sixth Edition, c Pearson Education, Inc. All rights reserved. Sixth Edition. or it is divisible by . Inc.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. c Pearson Education. A year is a leap year if it is divisible by but not by . Introduction to Java Programming. year ampamp year year LeapYear Run Liang. . All rights reserved. as shown below. The program randomly generates two singledigit integers number and number and displays a question such as What is to the student. After the student types the answer in the input dialog box. Inc. the program displays a message dialog box to indicate whether the answer is true or false. c Pearson Education. Sixth Edition. .Example A Simple Math Learning Tool This example creates a program to let a first grader practice additions. AdditionTutor Run Liang. All rights reserved. Introduction to Java Programming. The amp and Operators ampamp conditional AND operator amp unconditional AND operator conditional OR operator unconditional OR operator exp ampamp exp lt x ampamp x lt lt x amp x lt Liang. c Pearson Education. All rights reserved. Sixth Edition. Introduction to Java Programming. Inc. . Introduction to Java Programming. Inc. what is x after this expression gt x ampamp gt x How about x gt x x gt x Liang. c Pearson Education. what is x after this expression x gt amp x lt If x is . Sixth Edition.The amp and Operators If x is . All rights reserved. . Inc. . All rights reserved. Introduction to Java Programming.Selection Statements if Statements switch Statements Conditional Operators Liang. c Pearson Education. Sixth Edition. .out. A B Liang. Sixth Edition. System.Simple if Statements if booleanExpression statements.printlnquotThe area for the circle of quot quotradius quot radius quot is quot area. Boolean Expression true Statements false radius false true area radius radius PI. c Pearson Education.out. Introduction to Java Programming.printlnquotThe areaquot quot for the circle of radius quot radius quot is quot area. if radius area radius radius PI. All rights reserved. Inc. System. b Liang. Sixth Edition. . c Pearson Education.out.printlnquoti is an quot quotinteger between and quot. Introduction to Java Programming. a if i gt ampamp i lt System.out.printlnquoti is an quot quotinteger between and quot. All rights reserved. Inc.Note Outer parentheses required races can be omitted i the block contains a single statement quivalent if i gt ampamp i lt System. Sixth Edition. This mistake is hard to find. it is a logic error. Inc. Introduction to Java Programming.out.Caution Adding a semicolon at the end of an if clause is a common mistake. Liang. because it is not a compilation error or a runtime error. c Pearson Education. System.println quotThe area for the circle of radius quot radius quot is quot area. This error often occurs when you use the nextline block style. if radius . All rights reserved. Wrong area radiusradiusPI. . else Statement if booleanExpression statementsforthetruecase. else statementsforthefalsecase. . Inc. Introduction to Java Programming. c Pearson Education. Sixth Edition...The if. true Boolean Expression false Statements for the true case Statements for the false case Liang. All rights reserved. System..else Example if radius gt area radius radius . Introduction to Java Programming. c Pearson Education. .out..out. Inc.. All rights reserved. Liang.printlnquotNegative inputquot. Sixth Edition. else System.if.printlnquotThe area for the circle of radius quot radius quot is quot area. else if score gt . grade C. else if score gt . else if score gt . else grade F. grade B. grade D. Introduction to Java Programming. grade D. Equivalent Liang. else if score gt . c Pearson Education. if score gt . . Sixth Edition.Multiple Alternative if Statements if score gt . All rights reserved. Inc. grade C. grade A. grade A. else if score gt . else if score gt . grade B. else grade F. Liang.animation Trace ifelse statement The condition is false Suppose score is . Introduction to Java Programming. All rights reserved. else if score . Sixth Edition. if score . . else if score grade D. grade C. . grade A. else grade F. else if score . grade B. c Pearson Education. Inc. . Introduction to Java Programming.animation Trace ifelse statement The condition is false Suppose score is . if score . Inc. grade C. else if score . Liang. Sixth Edition. grade A. c Pearson Education. else if score grade D. else if score . grade B. . All rights reserved. else grade F. . grade A. else if score grade D. else if score . grade B. else if score . Liang. Introduction to Java Programming. grade C. c Pearson Education. . if score .animation Trace ifelse statement The condition is true Suppose score is . Inc. All rights reserved. else grade F. Sixth Edition. All rights reserved. Introduction to Java Programming. else if score . . Inc.animation Trace ifelse statement grade is C Suppose score is . . Liang. else if score . Sixth Edition. c Pearson Education. if score . grade C. grade A. else if score grade D. else grade F. grade B. Sixth Edition. if score . c Pearson Education. else if score . . Inc. Liang. grade B. . grade A. Introduction to Java Programming. else if score grade D. else grade F. grade C. All rights reserved. else if score .animation Trace ifelse statement Exit the if statement Suppose score is . out. if i gt j if i gt k System.printlnquotBquot.printlnquotAquot. c Pearson Education.out. a Liang. int k . Sixth Edition.Note The else clause matches the most recent if clause in the same block.printlnquotBquot. Equivalent int i . Introduction to Java Programming. . int i . int j . All rights reserved. else System.out. else System.printlnquotAquot. int k .out. int j . b if i gt j if i gt k System. Inc. Note. you must add a pair of braces int i . if i gt j if i gt k System. c Pearson Education. Liang.printlnquotAquot. Introduction to Java Programming. else System. All rights reserved. This statement prints B.printlnquotBquot. Sixth Edition. Nothing is printed from the preceding statement. cont. int j . Inc. int k .out. .out. To force the else clause to match the first if clause. else even false. b Liang. a quivalent boolean even number . All rights reserved. Inc. c Pearson Education. Sixth Edition.TIP if number even true. . Introduction to Java Programming. Introduction to Java Programming. c Pearson Education.CAUTION if even true System.println quotIt is even.quot. All rights reserved. b Liang.out. Sixth Edition.println quotIt is even.quot. a quivalent if even System.out. Inc. . The tax rates for are shown in Table . Inc. c Pearson Education.. All rights reserved. married filing separately. and head of household. There are four filing statuses single filers. Sixth Edition. Introduction to Java Programming. Liang.Example Computing Taxes The US federal personal income tax is calculated based on the filing status and taxable income. . married filing jointly. Example Computing Taxes. Sixth Edition. cont. Introduction to Java Programming. All rights reserved. Inc. if status // Compute tax for single filers else if status // Compute tax for married file jointly else if status // Compute tax for married file separately else if status // Compute tax for head of household else // Display wrong status ComputeTaxWithSelectionStatement Liang. c Pearson Education. Run . Example An Improved Math Learning Tool This example creates a program to teach a first grade child how to learn subtractions. Introduction to Java Programming. as shown in figure. the program displays a message dialog box to indicate whether the answer is correct. Sixth Edition. Inc. All rights reserved. . SubtractionTutor Run Liang. c Pearson Education. as shown in the figure. After the student types the answer in the input dialog box. The program randomly generates two singledigit integers number and number with number number and displays a question such as What is to the student. GuessBirthDate Ru Liang.Example Guessing Birth Date The program can guess your birth date. c Pearson Education. Sixth Edition. All rights reserved. Introduction to Java Programming. Run to see how it works. . Inc. case compute taxes for head of household. System. All rights reserved. Sixth Edition. break. . Liang. c Pearson Education.switch Statements switch status case compute taxes for single filers. break. Introduction to Java Programming. Inc.exit.printlnquotErrors invalid statusquot.out. default System. break. case compute taxes for married file separately. break. case compute taxes for married file jointly. switch Statement Flow Chart status is Compute tax or single ilers status is Compute tax or married ile ointly status is Compute tax or married ile separatly break break break status is Compute tax or head o household de ault e ault actions break ext Statement Liang. Inc. All rights reserved. . c Pearson Education. Sixth Edition. Introduction to Java Programming. byte. c Pearson Education. meaning that they cannot contain variables in the expression... break. .. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switchexpression.switch Statement Rules The switchexpression must yield a value of char. or int type and must always be enclosed in parentheses. Liang. and valueN are constant expressions. case value statements.. break. Note that value. break. short. . and valueN must have the same data type as the value of the switchexpression. such as x. Inc. All rights reserved. switch switchexpression case value statements. case valueN statementsN. Sixth Edition. The value. . Introduction to Java Programming... default statementsfordefault. which is optional. case value statements. but the order of the cases including the default case does not matter. The default case.switch Statement Rules The keyword break is optional. switch switchexpression case value statements. If the break statement is not present. the next case statement will be executed. it is good programming style to follow the logical sequence of the cases and place the default case at the end. default statementsfordefault. Introduction to Java Programming. . Sixth Edition. break. but it should be used at the end of each case in order to terminate the remainder of the switch statement. c Pearson Education. The case statements are executed in sequential order. break. Liang. All rights reserved. can be used to perform actions when none of the specified cases matches the switchexpression. Inc. case valueN statementsN. break. However. out.printlnch. System.out. Liang. Introduction to Java Programming.printlnch.animation Trace switch statement Suppose ch is a switch case case case ch a b c System.out. . System. c Pearson Education. Sixth Edition.printlnch. Inc. All rights reserved. out. Introduction to Java Programming. System. System.printlnch.printlnch. Sixth Edition. All rights reserved.out. Liang. Inc.out. .printlnch.animation Trace switch statement ch is a switch case case case ch a b c System. c Pearson Education. Inc.printlnch. c Pearson Education. All rights reserved. Introduction to Java Programming.out.out.printlnch.printlnch. System.animation Trace switch statement Execute this line switch case case case ch a b c System. System. . Liang.out. Sixth Edition. printlnch. .printlnch. System.out.out.out.printlnch. Introduction to Java Programming.animation Trace switch statement Execute this line switch case case case ch a b c System. c Pearson Education. Inc. Liang. Sixth Edition. System. All rights reserved. animation Trace switch statement Execute this line switch case case case ch a b c System.printlnch. Inc. c Pearson Education. .printlnch.out. Sixth Edition. All rights reserved.printlnch. Introduction to Java Programming. System. Liang.out. System.out. printlnch. System. Inc. System.out. . Introduction to Java Programming.animation Trace switch statement Execute next statement switch case case case ch a b c System. All rights reserved.out.printlnch. Sixth Edition. Liang.printlnch. Next statement. c Pearson Education.out. case c System. Liang.printlnch.animation Trace switch statement Suppose ch is a switch ch case a System.printlnch.out. break.out. Sixth Edition. break.out. c Pearson Education.printlnch. Inc. case b System. All rights reserved. . Introduction to Java Programming. printlnch. Inc. All rights reserved.out. break. Sixth Edition. . c Pearson Education.out.printlnch. break. case b System. Liang.animation Trace switch statement ch is a switch ch case a System.printlnch.out. case c System. Introduction to Java Programming. .out. All rights reserved.printlnch.animation Trace switch statement Execute this line switch ch case a System. break. break. Sixth Edition. c Pearson Education. Liang.out. Inc.out.printlnch.printlnch. case b System. case c System. Introduction to Java Programming. All rights reserved.printlnch.out.printlnch. c Pearson Education.animation Trace switch statement Execute this line switch ch case a System.out.out. break. break.printlnch. Introduction to Java Programming. case b System. Liang. Sixth Edition. . case c System. Inc. Introduction to Java Programming. case b System. Sixth Edition. break.out. .printlnch. Next statement. All rights reserved. Liang.out.printlnch. Inc. case c System.out. break.printlnch.animation Trace switch statement Execute next statement switch ch case a System. c Pearson Education. c Pearson Education. Introduction to Java Programming. Sixth Edition. All rights reserved.Conditional Operator if x y else y . Inc. is equivalent to y x . booleanExpression expression expression Ternary operator Binary operator Unary operator Liang. . Sixth Edition.out. All rights reserved. System.println num num is even num is odd.out. c Pearson Education. Inc. Liang.printlnnum is odd.printlnnum is even. else System. Introduction to Java Programming.out. .Conditional Operator if num System. c Pearson Education. Introduction to Java Programming. booleanExp exp exp Liang. All rights reserved. Sixth Edition.Conditional Operator. Inc. cont. . Feature Formatting Output Use the new JDK . Inc. items. boolean value. Introduction to Java Programming. A format specifier specifies how an item should be displayed. An item may be a numeric value. Sixth Edition.JDK .out. Each specifier begins with a percent sign. Where format is a string that may consist of substrings and format specifiers. printf statement. or a string. c Pearson Education. . character. Liang.printfformat. All rights reserved. System. double amount .printfquotcount is d and amount is fquot. . Introduction to Java Programming..out. display count is and amount is . Inc. items System. c Pearson Education.e quotJava is coolquot a boolean value a character a decimal integer a floatingpoint number a number in standard scientific notation Specifier Output b c d f e s a string int count . Sixth Edition.JDK . Liang. All rights reserved. Feature FrequentlyUsed Specifiers Example true or false a . . count. amount. . .. Inc.. Sixth Edition..formatformat. item. item..Creating Formatted Strings System. itemk String s String. itemk String.. . Introduction to Java Programming. item.out. . . .formatquotcount is d and amount is fquot. c Pearson Education. All rights reserved.printfformat. item. Liang.. All rights reserved. Inc. Sixth Edition.Operator Precedence How to evaluate Liang. c Pearson Education. . Introduction to Java Programming. var. . . Introduction to Java Programming. gt.Unary plus and minus.var type Casting Not . Inc. /. gt Comparison . Sixth Edition. . . Equality amp Unconditional AND Exclusive OR Unconditional OR ampamp Conditional AND Shortcircuit AND Conditional OR Shortcircuit OR . .Binary addition and subtraction lt. Multiplication. All rights reserved. c Pearson Education. Assignment operator Liang. and remainder .Operator Precedence var. lt. division. var. . /. . c Pearson Education. the operators are applied according to the precedence rule and the associativity rule. Parentheses can be nested. When evaluating an expression without parentheses. . All rights reserved. If operators with the same precedence are next to each other.Operator Precedence and Associativity The expression in the parentheses is evaluated first. their associativity determines the order of evaluation. Sixth Edition. in which case the expression in the inner parentheses is executed first. Liang. Inc. Introduction to Java Programming. All binary operators except assignment operators are leftassociative. c Pearson Education.Operator Associativity When two operators with the same precedence are evaluated. the expression a b c is equivalent to a b c Liang. . Sixth Edition. All binary operators except assignment operators are leftassociative. the associativity of the operators determines the order of evaluation. a b c d is equivalent to a b c d Assignment operators are rightassociative. All rights reserved. Therefore. Introduction to Java Programming. Inc. the expression . gt gt multiplication inside parentheses first multiplication gt addition gt subtraction gt greater than false Liang. Introduction to Java Programming. Inc. c Pearson Education. All rights reserved. . Sixth Edition.Example Applying the operator precedence and associativity rule. is evaluated as follows gt . Sixth Edition.Operand Evaluation Order The precedence and associativity rules specify the order of the operators. but do not specify the order in which the operands of a binary operator are evaluated. Inc. c Pearson Education. Liang. Operands are evaluated from left to right in Java. All rights reserved. . The lefthand operand of a binary operator is evaluated before any part of the righthand operand is evaluated. Introduction to Java Programming. Liang. If no operands have side effects that change the value of a variable. a a.Operand Evaluation Order. because a is evaluated to before a is evaluated to . But x becomes in the following code. the order of operand evaluation is irrelevant. int a int x . Sixth Edition. then a is evaluated to . x becomes in the following code. Introduction to Java Programming. . For example. All rights reserved. cont. int a int x . Interesting cases arise when operands do have a side effect. because a is evaluated to . a a. Inc. c Pearson Education. Rule of Evaluating an Expression Rule Evaluate whatever subexpressions you can possibly evaluate from left to right. Rule The associativity rule applies for two operators next to each other with the same precedence. Liang. Rule The operators are applied according to their precedence. c Pearson Education. Inc. Sixth Edition. Introduction to Java Programming. All rights reserved. . is evaluated now. gt . gt . Inc. Sixth Edition. gt is evaluated now. gt . Introduction to Java Programming. the expression is evaluated as follows gt .Rule of Evaluating an Expression Applying the rule. gt is evaluated now. false Liang. is the first subexpression that can be evaluated from left. is evaluated now. is now the leftmost subexpression that should be evaluated. All rights reserved. . c Pearson Education.