Chapter 3:Decision Structures - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Chapter 3:Decision Structures

Description:

Comparing String Objects. In most cases, you cannot use the relational operators to compare two String objects. ... Ignoring Case in String Comparisons ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 33
Provided by: lor81
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3:Decision Structures


1
Chapter 3Decision Structures
2
Chapter 3Decision Structures
  • 3.1 The if Statement
  • 3.2 The if-else Statement
  • 3.3 The if-else-if Statement 
  • 3.4 Nested if Statements 
  • 3.5 Logical Operators
  • 3.6 Comparing String Objects
  • 3.7 More About Variable Declaration and Scope
  • 3.9 The switch Statement
  • 3.10 Creating Objects with the DecimalFormat
    Class
  • 3.12 Common Errors to Avoid

3
Nested if Statements
  • If an if statement appears inside of another if
    statement (single or block) it is called a nested
    if statement.
  • The nested if is only executed if the if
    statement it is in results in a true condition.
  • Nested if statements can get very complex, very
    quickly.

4
LoanQualifier.java
  • double salary, yearsOnJob
  • String input
  • input JOptionPane.showInputDialog("Enter your "
    "annual salary.")
  • salary Double.parseDouble(input)
  • input JOptionPane.showInputDialog("Enter the
    number of " "years at your current job.")
  • yearsOnJob Double.parseDouble(input)

5
LoanQualifier.java
  • if (salary gt 30000)
  • if (yearsOnJob gt 2)
  • JOptionPane.showMessageDialog(null, "You
    qualify " "for the loan.")
  • else
  • JOptionPane.showMessageDialog(null, "You
    must have " "been on your current job for
    at least "
  • "two years to qualify.")
  • else
  • JOptionPane.showMessageDialog(null, "You must
    earn at least 30,000 per year to qualify.")

6
Nested if Statement Flowcharts
7
if-else Matching
  • Curly brace use is not required if there is only
    one statement to be conditionally executed.
  • However, sometimes curly braces can help make the
    program more readable.
  • Additionally, proper indentation makes it much
    easier to match up else statements with their
    corresponding if statement.

8
if-else Matching
  • if (employed 'y')
  • if (recentGrad 'y')
  • System.out.println("You qualify for the special
    interest rate.")
  • else
  • System.out.println("You must be a recent
    college graduate to qualify.")
  • else
  • System.out.println("You must be employed to
    qualify.")

This else matches with this if.
This else matches with this if.
9
Nested if Statements
  • If an if statement appears inside of another if
    statement (single or block) it is called a nested
    if statement.
  • The nested if is only executed if the if
    statement it is in results in a true condition.
  • Nested if statements can get very complex, very
    quickly.

10
LoanQualifier.java
  • double salary, yearsOnJob
  • String input
  • input JOptionPane.showInputDialog("Enter your "
    "annual salary.")
  • salary Double.parseDouble(input)
  • input JOptionPane.showInputDialog("Enter the
    number of " "years at your current job.")
  • yearsOnJob Double.parseDouble(input)

11
LoanQualifier.java
  • if (salary gt 30000)
  • if (yearsOnJob gt 2)
  • JOptionPane.showMessageDialog(null, "You
    qualify " "for the loan.")
  • else
  • JOptionPane.showMessageDialog(null, "You
    must have " "been on your current job for
    at least "
  • "two years to qualify.")
  • else
  • JOptionPane.showMessageDialog(null, "You must
    earn at least 30,000 per year to qualify.")

12
Nested if Statement Flowcharts
13
if-else Matching
  • Curly brace use is not required if there is only
    one statement to be conditionally executed.
  • However, sometimes curly braces can help make the
    program more readable.
  • Additionally, proper indentation makes it much
    easier to match up else statements with their
    corresponding if statement.

14
if-else Matching
  • if (employed 'y')
  • if (recentGrad 'y')
  • System.out.println("You qualify for the special
    interest rate.")
  • else
  • System.out.println("You must be a recent
    college graduate to qualify.")
  • else
  • System.out.println("You must be employed to
    qualify.")

This else matches with this if.
This else matches with this if.
15
Logical Operators
  • Java provides two binary logical operators (
    and ) that are used to combine boolean
    expressions.
  • Java also provides one unary (!) logical operator
    to reverse the truth of a boolean expression.

16
Logical Operators
17
The Operator
  • The logical AND operator () takes two operands
    that must both be boolean expressions.
  • The resulting combined expression is true iff (if
    and only if) both operands are true.

18
LogicalAND
  • double salary // Annual salary
  • double yearsOnJob // Years at current
    job
  • String input // To hold string
    input
  • input JOptionPane.showInputDialog("Enter
    your " "annual salary.")
  • salary Double.parseDouble(input)
  • input JOptionPane.showInputDialog(
    "Enter the number of " "years at your
    current job.")
  • yearsOnJob Double.parseDouble(input)

19
LogicalAND
  • // Determine whether the user qualifies for the
    loan.
  • if (salary gt 30000 yearsOnJob gt 2)
  • JOptionPane.showMessageDialog(null,
    "You qualify " "for the loan.")
  • else
  • JOptionPane.showMessageDialog(null,
  • "You do not " "qualify for the loan.")

20
The Operator
  • The logical OR operator () takes two operands
    that must both be boolean expressions.
  • The resulting combined expression is false iff
    (if and only if) both operands are false.

21
LogicalOR
  • double salary // Annual salary
  • double yearsOnJob // Years at current
    job
  • String input // To hold string
    input
  • input JOptionPane.showInputDialog("Enter
    your " "annual salary.")
  • salary Double.parseDouble(input)
  • input JOptionPane.showInputDialog(
    "Enter the number of " "years at your
    current job.")
  • yearsOnJob Double.parseDouble(input)

22
LogicalOR
  • // Determine whether the user qualifies for the
    loan.
  • if (salary gt 30000 yearsOnJob gt 2)
  • JOptionPane.showMessageDialog(null,
  • "You qualify " "for the
    loan.")
  • else
  • JOptionPane.showMessageDialog(null,
    "You do not " "qualify for the
    loan.")

23
The ! Operator
  • The ! operator performs a logical NOT operation.
  • If an expression is true, !expression will be
    false.
  • if (!(temperature gt 100))
  • System.out.println(Below the maximum
    temperature.")
  • If temperature gt 100 evaluates to false, then the
    output statement will be run.

24
Short Circuiting
  • Logical AND and logical OR operations perform
    short-circuit evaluation of expressions.
  • Logical AND will evaluate to false as soon as it
    sees that one of its operands is a false
    expression.
  • Logical OR will evaluate to true as soon as it
    sees that one of its operands is a true
    expression.

25
Order of Precedence
  • The ! operator has a higher order of precedence
    than the and operators.
  • The and operators have a lower precedence
    than relational operators like lt and gt.
  • Parenthesis can be used to force the precedence
    to be changed.

26
Order of Precedence
27
Comparing String Objects
  • In most cases, you cannot use the relational
    operators to compare two String objects.
  • Reference variables contain the address of the
    object they represent.
  • Unless the references point to the same object,
    the relational operators will not return true.
  • StringCompareTo.java

28
StringCompare.java
  • String name1 "Mark",
  • name2 "Mark",
  • name3 "Mary"
  • // Compare "Mark" and "Mark"
  • if (name1.equals(name2))
  • System.out.println(name1 " and "
    name2
  • " are the same.")
  • else
  • System.out.println(name1 " and "
    name2
  • " are the NOT the
    same.")

29
StringCompare.java
  • // Compare "Mark" and "Mary"
  • if (name1.equals(name3))
  • System.out.println(name1 " and "
    name3
  • " are the same.")
  • else
  • System.out.println(name1 " and "
    name3
  • " are the NOT the
    same.")

30
StringCompareTo.java
  • String name1 "Mary",
  • name2 "Mark"
  • // Compare "Mary" and "Mark"
  • if (name1.compareTo(name2) lt 0)
  • System.out.println(name1 " is less
    than " name2)
  • else if (name1.compareTo(name2) 0)
  • System.out.println(name1 " is equal to
    " name2)
  • else if (name1.compareTo(name2) gt 0)
  • System.out.println(name1 " is greater
    than " name2)

31
Ignoring Case in String Comparisons
  • In the String class the equals and compareTo
    methods are case sensitive.
  • In order to compare two String objects that might
    have different case, use
  • equalsIgnoreCase, or
  • compareToIgnoreCase

32
SecretWord.java
  • if (input.equalsIgnoreCase("PROSPERO"))
  • System.out.println("Congratulations!
    You know the secret word!")
  • else
  • System.out.println("Sorry, that is NOT
    the " "secret word!")
Write a Comment
User Comments (0)
About PowerShow.com