1 / 47

Java Programming Sixth Edition Chapter 5: Making Decisions

Java Programming Sixth Edition Chapter 5: Making Decisions. Objectives. Plan decision-making logic Make decisions with the if and if...else structures Use multiple statements in an if or if...else structure Nest if and if...else statements Use AND and OR operators.

meena
Download Presentation

Java Programming Sixth Edition Chapter 5: Making Decisions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java ProgrammingSixth EditionChapter 5: Making Decisions

  2. Objectives • Plan decision-making logic • Make decisions with the if and if...else structures • Use multiple statements in an if or if...else structure • Nest if and if...else statements • Use AND and OR operators Java Programming, Sixth Edition

  3. Objectives (cont'd.) • Make accurate and efficient decisions • Use the switch statement • Use the conditional and NOT operators • Assess operator precedence Java Programming, Sixth Edition

  4. Planning Decision-Making Logic • Pseudocode • Use paper and pencil • Plan program’s logic • By writing plain English statements • Accomplish important steps in a given task • Use everyday language • Flowchart • Steps in diagram form • Series of shapes connected by arrows Java Programming, Sixth Edition

  5. Planning Decision-Making Logic (cont'd.) • Flowchart (cont'd.) • Programmers use variety of shapes to represent different tasks • Rectangle to represent any unconditional step • Diamond to represent any decision • Sequence structure • One step follows another unconditionally • Cannot branch away or skip a step Java Programming, Sixth Edition

  6. Java Programming, Sixth Edition

  7. Planning Decision-Making Logic (cont'd.) • Decision structure • Involves choosing between alternative courses of action • Based on some value within program • All computer decisions are yes-or-no decisions • Boolean values • Values true and false • Used in every computer decision Java Programming, Sixth Edition

  8. Java Programming, Sixth Edition

  9. The if and if...else Structures • if statement • Simplest statement to make decision • Boolean expression appears within parentheses • Space between keyword if and opening parenthesis • Execution always continues to next independent statement • Use double equal sign (==) to determine equivalency Java Programming, Sixth Edition

  10. The if and if...else Structures (cont'd.) Java Programming, Sixth Edition

  11. Pitfall: Misplacing a Semicolon in an if Statement • No semicolon at end of first line of if statement • if (someVariable == 10) • Statement does not end there • When semicolon follows if directly • Empty statement contains only semicolon • Execution continues with the next independent statement Java Programming, Sixth Edition

  12. Pitfall: Misplacing a Semicolon in an if Statement (cont'd.) Java Programming, Sixth Edition

  13. Pitfall: Using the Assignment Operator Instead of the Equivalency Operator • Attempt to determine equivalency • Using single equal sign • Rather than double equal sign • Illegal • Can store Boolean expression’s value in Boolean variable • Before using in if statement Java Programming, Sixth Edition

  14. Pitfall: Attempting to Compare Objects Using the Relational Operators • Use standard relational operators to compare values of primitive data types • Not objects • Can use equals and not equals comparisons (== and !=) with objects • To compare objects’ memory addresses instead of values Java Programming, Sixth Edition

  15. The if...else Structure • Single-alternative if • Only perform action, or not • Based on one alternative • Dual-alternative if • Two possible courses of action • if...else statement • Performs one action when Boolean expression evaluates true • Performs different action when Boolean expression evaluates false Java Programming, Sixth Edition

  16. The if...else Structure (cont'd.) • if...else statement (cont'd.) • Statement that executes when if is true or false ends with semicolon • Vertically align keyword if with the keyword else • Illegal to code else without if • Depending on evaluation of Boolean expression following if • Only one resulting action takes place Java Programming, Sixth Edition

  17. The if...else Structure (cont'd.) Java Programming, Sixth Edition

  18. Using Multiple Statementsin an if or if...else Structure • Execute more than one statement • Use pair of curly braces • Place dependent statements within block • Crucial to place curly braces correctly • Any variable declared within block local to that block Java Programming, Sixth Edition

  19. Using Multiple Statementsin an if or if...else Structure (cont’d.) Java Programming, Sixth Edition

  20. Nesting if and if...else Statements • Nested if statements • Statements in which if structure is contained inside another if structure • Use when two conditions must be met before some action is taken • Pay careful attention to placement of else clauses • else statements • Always associated with if on “first in-last out” basis Java Programming, Sixth Edition

  21. Nesting if and if...else Statements (cont'd.) Java Programming, Sixth Edition

  22. Using Logical AND and OR Operators • Logical AND operator • Alternative to some nested if statements • Used between two Boolean expressions to determine whether both are true • Written as two ampersands (&&) • Include complete Boolean expression on each side • Both Boolean expressions that surround operator • Must be true before action in statement can occur • Short-circuit evaluation • Expressions on each side of the logical operator are evaluated only as far as necessary Java Programming, Sixth Edition

  23. Using Logical AND and OR Operators (cont'd.) Java Programming, Sixth Edition

  24. Using Logical AND and OR Operators (cont'd.) • Conditional OR operator • Action to occur when at least one of two conditions is true • Written as || • Sometimes called pipes Java Programming, Sixth Edition

  25. Using Logical AND and OR Operators (cont'd.) Java Programming, Sixth Edition

  26. Making Accurate and Efficient Decisions • Range check • Series of if statements that determine whether: • Value falls within specified range • Java programmers commonly place each else of subsequent if on same line • Within nested if...else • Most efficient to ask most likely question first • Avoid asking multiple questions Java Programming, Sixth Edition

  27. Making Accurate and Efficient Decisions (cont'd.) Java Programming, Sixth Edition

  28. Using && and || Appropriately • Errors of beginning programmers • Using AND operator when they mean to use OR • Example: no payRate value can ever be both below 5.65 and over 60 at same time if(payRate < LOW && payRate > HIGH) System.out.println("Error in pay rate"); • Use pipes “||” operator instead • Using single ampersand or pipe • To indicate logical AND or OR Java Programming, Sixth Edition

  29. Using the switch Statement • switch statement • Alternative to series of nested if statements • Test single variable against series of exact integer, character, or string values Java Programming, Sixth Edition

  30. Using the switch Statement (cont'd.) • Keywords • switch • Starts structure • Followed by test expression • Enclosed in parentheses • case • Followed by one of the possible values for test expression and colon Java Programming, Sixth Edition

  31. Using the switch Statement (cont'd.) • Keywords (cont'd.) • break • Optionally terminates switch structure at end of each case • default • Used prior to any action if test variable does not match any case • Optional Java Programming, Sixth Edition

  32. Using the switch Statement (cont'd.) Java Programming, Sixth Edition

  33. Using the switch Statement (cont'd.) • break statements in switch structure • If break statement omitted • Program finds match for test variable • All statements within switch statement execute from that point forward • case statement • No need to write code for each case • Evaluate char variables • Ignore whether uppercase or lowercase Java Programming, Sixth Edition

  34. Using the switch Statement (cont'd.) • Why use switch statements? • Convenient when several alternative courses of action depend on single integer, character, or string value • Use only when there are reasonable number of specific matching values to be tested Java Programming, Sixth Edition

  35. Using the Conditionaland NOT Operators • Conditional operator • Requires three expressions • Separated with question mark and colon • Used as abbreviated version of if...else structure • Never required to use • Syntax of conditional operator testExpression ? trueResult : falseResult; Java Programming, Sixth Edition

  36. Using the Conditionaland NOT Operators (cont'd.) • Boolean expression evaluated as true or false • If testExpression value true • Entire conditional expression takes on value of expression following question mark • If value false • Entire expression takes on value of falseResult • Advantage of using conditional operator • Conciseness of statement Java Programming, Sixth Edition

  37. Using the NOT Operator • NOT operator • Written as exclamation point (!) • Negates result of any Boolean expression • When preceded by NOT operator • Any expression evaluates as: • true becomes false • false becomes true • Statements with NOT operator • Harder to read • Require double set of parentheses Java Programming, Sixth Edition

  38. Understanding Operator Precedence • Combine as many AND or OR operators as needed • Operator’s precedence • How expression is evaluated • Order agrees with common algebraic usage • Arithmetic done first • Assignment done last • AND operator evaluated before OR operator • Statements in parentheses evaluated first Java Programming, Sixth Edition

  39. Understanding Operator Precedence (cont'd.) Java Programming, Sixth Edition

  40. Understanding Operator Precedence (cont'd.) • Two important conventions • Order in which operators are used • Always use parentheses • Change precedence • Or make your intentions clearer Java Programming, Sixth Edition

  41. Understanding Operator Precedence (cont'd.) Java Programming, Sixth Edition

  42. You Do It • Using an if...else • Creating an Event class to use in a decision-making application • Writing an application that uses the Event class • Using the switch statement Java Programming, Sixth Edition

  43. Don’t Do It • Don’t ignore subtleties in boundaries used in decision making • Don’t use the assignment operator instead of the comparison operator • Don’t insert a semicolon after the Boolean expression in an if statement • Don’t forget to block a set of statements with curly braces when several statements depend on the if or the else statement Java Programming, Sixth Edition

  44. Don’t Do It (cont'd.) • Don’t forget to include a complete Boolean expression on each side of an && or || operator • Don’t try to use a switch structure to test anything other than an integer, character, or string value • Don’t forget a break statement if one is required • Don’t use the standard relational operators to compare objects Java Programming, Sixth Edition

  45. Summary • if statement • Make decision based on Boolean expression • Single-alternative if • Performs action based on one alternative • Dual-alternative if • if...else • Performs one action when a Boolean expression evaluates as true • Performs different action when expression evaluates as false Java Programming, Sixth Edition

  46. Summary (cont'd.) • AND operator • && • Determines whether two expressions are both true • OR operator • || • Carries out some action even if only one of two conditions is true • switch statement • Tests a single variable against a series of exact integer or character values Java Programming, Sixth Edition

  47. Summary (cont'd.) • Conditional operator • Abbreviated version of if...else statement • NOT operator • ! • Negates result of any Boolean expression • Operator precedence Java Programming, Sixth Edition

More Related