{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch05 - Chapter Five Decisions Chapter Goals To be able to...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Five: Decisions
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
To be able to implement decisions using if statements To understand how to group statements into blocks To learn how to compare integers, floating-point numbers, strings, and objects To recognize the correct ordering of decisions in multiple branches To program conditions using Boolean operators and variables To understand the importance of test coverage Chapter Goals
Image of page 2
The if statement lets a program carry out different actions depending on a condition If (amount <= balance) balance = balance – amount; The if Statement
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
If (amount <= balance) balance = balance – amount; else balance = balance – OVERDRAFT_PENALTY The if / else Statement
Image of page 4
Simple statement balance = balance - amount; Compound statement if (balance >= amount) balance = balance - amount; Also while, for , etc. (loop statements – Chapter 6) Block statement { double newBalance = balance - amount; balance = newBalance; } Statement Types
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
if( condition ) statement if ( condition ) statement 1 else Example: if (amount <= balance) balance = balance - amount; if (amount <= balance) balance = balance - amount; else Purpose: To execute a statement when a condition is true or false. Syntax 5.1 The if Statement
Image of page 6
{ statement 1 statement 2 . . . } Example: { double newBalance = balance - amount; balance = newBalance; } Purpose: To group several statements together to form a single statement. Syntax 5.2 Block Statement
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Why did we use the condition amount <= balance and not amount < balance in the example for the if/else statement? Answer: If the withdrawal amount equals the balance, the result should be a zero balance and no penalty. Self Check 5.1
Image of page 8
What is logically wrong with the statement if (amount <= balance) newBalance = balance - amount; balance = newBalance; and how do you fix it? Answer: Only the first assignment statement is part of the if statement. Use braces to group both assignment statements into a block statement. Self Check 5.2
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Relational operators compare values Java Math Notation Description > > Greater than >= Greater than or equal < < Less than <= Less than or equal == = Equal != Not equal The == denotes equality testing a = 5; // Assign 5 to a if (a == 5) . . . // Test whether a equals 5 Comparing Values: Relational Operators
Image of page 10
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 prints: sqrt(2)squared minus 2 is not 0 but 4.440892098500626E-16 Comparing Floating-Point Numbers
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
To avoid roundoff errors, don't use == to compare floating-point numbers To compare floating-point numbers test whether they are close enough : | x - y | ≤ ε final double EPSILON = 1E-14; if (Math.abs(x - y) <= EPSILON) // x is approximately equal to y ε is a small number such as 10 -14 Comparing Floating-Point Numbers
Image of page 12
Don't use == for strings!
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern