BOOK NOTES CHAPTER 6 6.1 THE IF STATEMENT - conditionally execute statements - implement a decision - if statement - parts - condition - body - syntax: if () - if condition is true, body executes - flow chart - implement a choice between alternatives - if/else statement - syntax: if () else - flow chart - combines statements to yield another statement - block statement - multiple statements in body - group several statements together to form a single statement - syntax: { } - consistently choose one of - line up braces - first brace at end of preceding line, last brace aligned with beginning of that line - always indent contents of block statement - simple statement - one line ending with semi-colon - indent if part of compound statement - compound statement - one control statement - one simple statement - body must be block, simple, or compound statement - selection operator - syntax: ? : - if condition true, evaluates to value1, else evaluates to value2 - combines values and yields another value 6.2 COMPARING VALUES - relational operators - tests the relationship btw two values - table: > greater than >= greater than or equal < less than < less than or equal == equal != not equal - compile-time error to compare different types (objects of different classes are considered different types) - comparing floating-point numbers - roundoff error - don't compare exactly - test if value close enough - | x - y | <= e - common epsilon 10^-14 - code: final double EPSILON = 1E-14; if (Math.abs(x - y) <= EPSILON) // x is approximately equal to y - comparing strings - == compares values of variables - values of object references are references - == only is true if it is the same object! - different objects can have the same corresponding values - use String method: boolean equals(String s2) - ignore case - String method - boolean equalsIgnoreCase(String s2) - compare unequal strings - String method - int compareTo(String s2) - less than 0 if implicit parameter comes lexicographically before arg - equal to 0 if the same - greater than 0 if implicit parameter lexicographically after arg - sorts space first, then numbers, capital letters, and lowercase letters - if implicit parameter substring of arg, arg considered after - comparing objects - == - tests whether references are the same (refer to same object) - does not matter whether contents match - use the equals method - must check that class implementor included it - we'll learn how in Chapter 13 - testing for null - common to use null to indicate value never set - use == to test if something is null - distinction between empty string and null 6.3 MULTIPLE ALTERNATIVES - sequences of comparisons - series of related comparisons - nesting - order matters - switch statement - analogous to sequence of is/else/else - test cases must be constants - test cases must be primitive integers, characters, or enumerated constants - syntax switch() { case : } - as soon as match, executes all following statements - break - exit in the middle of a statement - useful for executing only one option of a switch - obvious that all branches test the same value - nested branches - sometimes conditional need for more decisions - called nested when have one branch inside another - dangling else problem - compiler always associates else with closest if, regardless of indentation - use braces to force a different grouping - always use braces with nested branches - enumerated types - finite set of values - syntax: enum { } - naming convention - enum name should be named with a capital first letter and camel case - symbolic names should be all capital - can have any number of values - must include them all in the declaration - can declare variables of the enumerated type - use == to compare enumerated values - scope - common to nest declaration inside class - access outside of class where defined, use name of class as prefix - syntax: .. - variable of enum type can be null 6.4 USING BOOLEAN EXPRESSIONS - value of relational expression - true - false - boolean: type with only values true and false - predicate methods - method that returns a boolean value - use the return value of the method as a condition of an if statement - example: static predicate methods in Character class - common to preface name with "is" or "has" - Scanner class - avoid errors if user enters something unexpected - hasNextInt - hasNextDouble - boolean operators - logical operator combines test conditions - only passes if all of the conditions are true - "and" - operator: && - passes if at least one of the conditions is true - "or" - operator: || - flowcharts - invert a condition - "not" - operator: ! - takes a single condition - evaluates to true if the condition is false - evaluates to false if the condition is true - truth tables - operands can only be boolean expressions - common math-look-alike errors - x < y < z - x == y || z - short-circuit evaluation - logical expressions calculated from left to right - evaluation stops as soon as the truth value is determined - as soon as an and hits a false => false - as soon as an or hits a true => true - example - if (input != null && Integer.parseInt(input) > 0) - don't have to worry about second test barfing on null input - De Morgan's Law - simplify expressions with ! applied to && or || - !(A && B) <=> !A || !B - !(A || B) <=> !A && !B - && and || operators are reversed by distributing the ! - ! and relational operators - !(A < B) <=> A >= B - !(A > B) <=> A <= B - !(A <= B) <=> A > B - !(A >= B) <=> A < B - relational operators reverse and add/lose equality when not distributed - using boolean variables - use when only two possible values - store the outcome of a condition - aka flags b/c only "up" or "down" - think carefully about naming so when variable is true/false, meaning is obvious - bad style to compare a boolean variable to a boolean literal