More on if Statements - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

More on if Statements

Description:

if (x = = 0) . . // if x equals zero. The = operator assigns a value to a variable: ... double r = Math.sqrt(2); double d = (r * r) - 2; if (d == 0) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 12
Provided by: ally156
Category:

less

Transcript and Presenter's Notes

Title: More on if Statements


1
More on if Statements
  • Comparison operator

2
Equality Testing vs. Assignment
  • The operator tests for equalityif (x 0)
    . . // if x equals zero
  • The operator assigns a value to a variablex
    0 // assign 0 to x
  • Don't confuse them.

3
Comparing Floating-Point Numbers
  • Consider this code
  • double r Math.sqrt(2) double d (r r) -
    2
  • if (d 0)
  • System.out.println("sqrt(2)squared
    minus 2 is 0")
  • else
  • System.out.println( "sqrt(2)squared
    minus 2 is not 0 but " d)
  • It printssqrt(2)squared minus 2 is not 0 but
    4.440892098500626E-16
  • Don't use to compare floating-point numbers
  • Use gt or lt instead

4
String Comparison
  • Don't use for strings!if (input "Y") //
    WRONG!!!
  • Use equals methodif (input.equals("Y"))
  • tests identity, equals tests equal contents
  • Case insensitive test ("Y" or "y")if
    (input.equalsIgnoreCase("Y"))

5
  • public class StringExample
  • // instance variables - replace the example
    below with your own
  • private String name
  • private String nickname
  • /
  • Constructor for objects of class
    StringExample
  • /
  • public StringExample()
  • // initialize instance variables
  • name "Robert"
  • nickname name.substring(0,3)
  • public void displayNames()
  • if (nickname "Rob")

6
Object Comparison
  • tests for identity, equals for identical
    content
  • Rectangle cerealBox new Rectangle(5, 10, 20,
    30)Rectangle oatmealBox new Rectangle(5, 10,
    20, 30)Rectangle r cerealBox
  • cerealBox ! oatmealBox, but cerealBox.equals(oat
    mealBox)
  • cerealBox r
  • Note equals must be defined for the class

7
Object Comparison
8
Multiple Alternatives
  • if (condition1)
  • statement1
  • else if (condition2)
  • statement2
  • else if (condition3)
  • statement3
  • else
  • statement4
  • The first matching condition is executed.
  • Order matters.

9
  • public class Earthquake
  • private double richter
  • public Earthquake(double magnitude)
  • richter magnitude
  • public String getDescription()
  • String description
  • if (richter gt 8.0)
  • description "Most structures fall"
  • else if (richter gt 7.0)

10
Multiple Alternatives
  • Order matters
  • if (richter gt 0) // always passes
  • description "Generally not felt by
    people"
  • else if (richter gt 3.5) // not tested
  • description "Felt by many people, no
    destruction"
  • . . .
  • Be careful about omitting the else
  • if (richter gt 8.0)
  • description "Most structures fall"
  • if (richter gt 7.0) // omitted else--ERROR
  • description "Many buildings destroyed

11
Multiple Alternatives
  • The dangling else problem
  • if (richter gt 0)
  • if (richter lt 4)
  • System.out.println(The earthquake is
    harmless)
  • else
  • System.out.println(Negative value not
    allowed)
  • An else always belongs to the closest if.
  • Use braces to avoid this problem.
  • if (richter gt 0)
  • if (richter lt 4)
  • System.out.println(The earthquake is
    harmless)
  • else
Write a Comment
User Comments (0)
About PowerShow.com