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

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

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

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
If (amount <= balance) balance = balance – amount; else balance = balance – OVERDRAFT_PENALTY The if / else Statement
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background 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
Background image of page 7

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

View Full DocumentRight 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
Background 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
Background image of page 9

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

View Full DocumentRight 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
Background 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
Background image of page 11

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

View Full DocumentRight 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
Background image of page 12
Don't use == for strings! if (input == "Y") // WRONG!!!
Background image of page 13

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

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

This note was uploaded on 02/27/2009 for the course SC 418262 taught by Professor Chuleerut during the Spring '08 term at Kasetsart University.