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

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

Chapter Five: Decisions

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

View Full Document
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
The if statement lets a program carry out different actions depending on a condition If (amount <= balance) balance = balance – amount; The if Statement

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

View Full Document
If (amount <= balance) balance = balance – amount; else balance = balance – OVERDRAFT_PENALTY The if / else Statement
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

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

View Full Document
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
{ 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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
Don't use == for strings! if (input == "Y") // WRONG!!!

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.