Chapter 5

Decisions


Chapter Goals

The if Statement


The if/else Statement

if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;


Statement Types

Syntax 6.1: The if Statement

  if(condition)
   statement

if
(condition)
   statement1

else
   statement2

Example:

 
if (amount <= balance)
balance = balance - amount;

if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;

Purpose:

To execute a statement when a condition is true or false

Syntax 6.2: Block Statement

  {
   statement1
   statement2
   . . .

}


Example:

 
{
double newBalance = balance - amount;
balance = newBalance;
}

Purpose:

To group several statements together to form a single statement

Self Check

  1. Why did we use the condition amount <= balance and not amount < balance in the example for the if/else statement?
  2. What is logically wrong with the statement
    if (amount <= balance)
    newBalance = balance - amount; balance = newBalance;
    and how do you fix it?

Answers

  1. If the withdrawal amount equals the balance, the result should be a zero balance and no penalty
  2. Only the first assignment statement is part of the if statement. Use braces to group both assignment statements into a block statement

Comparing Values: Relational Operators

Comparing Floating-Point Numbers

Comparing Floating-Point Numbers

Comparing Strings

Lexicographic Comparison


Comparing Objects

Object Comparison


Testing for null

Self Check

  1. What is the value of s.length() if s is
    1. the empty string ""?
    2. the string " " containing a space?
    3. null?
  2. Which of the following comparisons are syntactically incorrect? Which of them are syntactically correct, but logically questionable?
    String a = "1";
    String b = "one";
    double x = 1;
    double y = 3 * (1.0 / 3);
    1. a == "1"
    2. a == null
    3. a.equals("")
    4. a == b
    5. a == x
    6. x == y
    7. x - y == null
    8. x.equals(y)

Answers

  1. (a) 0; (b) 1; (c) an exception is thrown
  2. Syntactically incorrect: e, g, h. Logically questionable: a, d, f

Multiple Alternatives: Sequences of Comparisons

File Earthquake.java

File EarthquakeTester.java


Output

   Enter a magnitude on the Richter scale: 7.1
   Many buildings destroyed

Multiple Alternatives: Nested Branches

Tax Schedule


If your filing status is Single
If your filing status is Married
Tax Bracket
Percentage
Tax Bracket
Percentage
$0 . . . $21,450
15%
0 . . . $35,800
15%
Amount over $21,450, up to $51,900
28%
Amount over $35,800, up to $86,500
28%
Amount over $51,900 31%
Amount over $86,500 31%
 

Nested Branches


File TaxReturn.java

File TaxReturnTester.java


Output

   Please enter your income: 50000
   Please enter S (single) or M (married): S
   The tax is 11211.5

Self Check

  1. The if/else/else statement for the earthquake strength first tested for higher values, then descended to lower values. Can you reverse that order?
  2. Some people object to higher tax rates for higher incomes, claiming that you might end up with less money after taxes when you get a raise for working hard. What is the flaw in this argument?

Answers

  1. Yes, if you also reverse the comparisons:
    if (richter < 3.5)
    r = "Generally not felt by people";
    else if (richter < 4.5)
    r = "Felt by many people, no destruction";
    else if (richter < 6.0)
    r = "Damage to poorly constructed buildings";
    . . .
  2. The higher tax rate is only applied on the income in the higher bracket. Suppose you are single and make $51,800. Should you try to get a $200 raise? Absolutely–you get to keep 72% of the first $100 and 69% of the next $100

Using Boolean Expressions: The boolean Type

Using Boolean Expressions: Predicate Method

Using Boolean Expressions: The Boolean Operators

&& and || Operators


Truth Tables

A
B
A && B
true
true
true
true
false
false
false
Any
false

A
B
A || B
true
Any
true
false
true
true
false
false
false

A
! A
true
false
false
true

Using Boolean Variables


Self Check

  1. When does the statement
    system.out.println (x > 0 || x < 0);
    print false?
  2. Rewrite the following expression, avoiding the comparison with false:
    if (Character.isDigit(ch) == false) . . .

Answers

  1. When x is zero
  2. if (!Character.isDigit(ch)) . . .