1 / 99

Chapter 3 Decision Structures

Chapter 3 Decision Structures. Contents. The if Statement The if-else Statement The if-else-if Statement Nested if Statement Logical Operators Comparing String Objects More about Variable Declaration and Scope. Contents (Cont’d). The Conditional Operators

tyra
Download Presentation

Chapter 3 Decision Structures

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. Chapter 3Decision Structures

  2. Contents • The if Statement • The if-else Statement • The if-else-if Statement • Nested if Statement • Logical Operators • Comparing String Objects • More about Variable Declaration and Scope

  3. Contents (Cont’d) • The Conditional Operators • The Switch Statement • Creating Objects with the DecimalFormat Class • The printf Method

  4. 1. The if Statement • Problem: • Write a program to calculate user’s average of 3 test scores. If the average is greater than 95, the program congratulates the users on obtaining a high score.

  5. 1. The if Statement (Cont’d)

  6. 1. The if Statement (Cont’d)

  7. 1. The if Statement (Cont’d) • Simple decision structure logic

  8. 1. The if Statement (Cont’d) if(BooleanExpression) statement; • The BooleanExpression must be a boolean expression • A boolean expression is either true or false • If the BooleanExpression is true, the very next statement is executed. Otherwise, it is skipped. • The statement is conditionally executed because it only executes under the condition that the expression in the parentheses is true.

  9. Using Relational Operators to Form Conditions • Typically, the boolean expression is formed with a relational operator (binary operator.

  10. Using Relational Operators to Form Conditions (Cont’d) • Assuming that a is 4, b is 6, and c is 4 • b>=atrue • c<=a true • a>=5 false • c==6 false • b!=6 false

  11. Programming Style and the if Statement • Two important style rules • The conditionally executed statement should appear on the line after the if statement. • The conditionally executed statement should be indented on level from the if statement. if(value>32) System.out.println(“Invalid number”); if(value>32) System.out.println(“Invalid number”);

  12. Be Careful with Semicolons No semicolon here if(BooleanExpression) statement; int x = 0, y = 10; if(x > y); System.out.println(x + “is greater than “ + y); Semicolon goes here It will always execute.

  13. Multiple Conditionally Executed Statements • Enclosing a group of statements by braces if(sales > 5000) { bonus = 500.0; commissionRate = 0.12; dayOff += 1; } These three statements are executed if sales is greater than 5000

  14. Comparing Characters • Using the relational operators to test character data as well as number • Assuming ch is a char variable: • Compare ch to the character ‘A’: if(ch==‘A’) System.out.println(“The letter is A.”); • Compare the ch is not equal to ‘A’: if(ch!=‘A’) System.out.println(“Not the letter is A.”);

  15. Comparing Characters (Cont’d) • In Unicode, letters are arranged in alphabetic order: • ‘A’ comes before ‘B’, the numeric code of ‘A’ (65) is less than the code of ‘B’ (66). • ‘A’ < ‘B’ true • In Unicode, the uppercase letters come before the lowercase letter.

  16. 2. The if-else statement • Problem • Write a program to get two numbers and divide the first number by the second number.

  17. 2. The if-else statement

  18. 2. The if-else statement

  19. Logic of the if-else Statement

  20. Logic of the if-else Statement (Cont’d) • The if-else statement will execute one group of statement if its boolean expression is true, or another group if its boolean expression is false. if(BooleanExpression) statement or block else statement or block

  21. 3. The if-else-if Statement • Problem • Write a program to ask the user to enter a numeric test score. Display a letter grade (A, B, C, D, or F) for the score. • score < 60 F • 60 <= score < 70 D • 70 <= score < 80 C • 80 <= score < 90 B • 90 <= score <= 100 A • score > 100 Invalid score

  22. Logic of the if-else-if Statement Logic of the if-else-if Statement

  23. 3. The if-else-if Statement • The if-else-if statement is a chain of if-else statements. Each statement in the chain performs its test until one of the tests is found to be true. if(BooleanExpression) statement or block else if(BooleanExpression) statement or block // //Put as many else if statement as needed here // else statement or block

  24. 4. Nested if Statement • Problem • Write a Java program to determine whether a bank customer qualifies for a loan. To qualify, a customer must earn at least $30,000 per year, and must have been on his or her current job for at least two years.

  25. 4. Nested if Statement • Input • User’s annual salary • Number of years at the current job

  26. 4. Nested if Statement (Cont’d) • An if statement appears inside another if statement, it is considered nested. • The rule for matching else clauses with if clauses is this: • An else clause goes with the closet previous if clause that doesn’t already have its own else clause.

  27. Alignment of if and else clauses

  28. 5. Logical Operators • Logical operators connect two or more relational expressions into one or reverse the logic of an expression. • Java provides • two binary logical operators • && : AND • || : OR • one unary logical operator • ! : NOT

  29. 5. Logical Operators (Cont’d)

  30. 5. Logical Operators (Cont’d)

  31. The && Operator • The && performs short-circuit evaluation • If the expression on the left side of the && operator is false, the expression on the right side will not be checked.

  32. The && Operator (Cont’d) • A different version of the LoanQualifier program

  33. The || Operator • The || performs short-circuit evaluation • If the expression on the left side of the || operator is true, the expression on the right side will not be checked.

  34. The || Operator • Problem • Write a Java program to determine whether a bank customer qualifies for a loan. To qualify, a customer must earn at least $30,000 per year, OR must have been on his or her current job for at least two years.

  35. The || Operator (Cont’d) • Input • User’s annual salary • Number of years at the current job

  36. A Better Solution

  37. The ! Operator • The ! Operator performs a logical NOT operation • !(x > 1000) x <= 1000

  38. The Precedence and Associativity of Logical Operators • The logical operators have orders of precedence and associativity. • The precedence of the logical operators, from highest to lowest ! && || • !(x > 2) • Applies the ! operator to the expression x > 2 • !x > 2 • Causes a compiler error

  39. The Precedence and Associativity of Logical Operators (Cont’d) • The && and || operators rank lower in precedence than the relational operators (a > b) && (x < y)a > b && x < y (x == y) || (b > a) x == y || b > a • The logical operators evaluate their expression from left to right • a < b || y == z • a < b is evaluated before y == z • a < b || y == z && m > j • y == z is evaluated first because the && operator has higher precedence than || • Is equivalent to the following (a < b) || ((y == z) && (m > j))

  40. The Precedence and Associativity of Logical Operators (Cont’d)

  41. Checking Numeric Ranges with Logical Operators • Determining whether a numeric value is within a specific range of values • Using the && operator x >= 20 && x <= 40 • Determining whether a numeric value is outside a specific range of values • Using the || operator x < 20 || x > 40

  42. 6. Comparing String Objects • We cannot use relational operators to compare String objects. Instead we must use a String method. String name1 = “Marks”; String name2 = “Mary”; • name1 == name2 will be false because the variables name1 and name2 reference different objects.

  43. 6. Comparing String Objects if(name1 == name2)

More Related