READING Chapter 5 Section 1 - if statement - specify which block of code to execute - 0+ statements - boolean expression - test condition - syntax if ( ) else - is a conditional expression that is evaluated to either true or false - executed if evaluates to true - executed if evaluates to false - relational operators < less than <= less than or equal to == equal to != not equal to > greater than >= greater than or equal to - reversing relational operator and switching the if and then blocks results in equivalent code - reverse of < is >=, not > - selection/branching statement - selects/branches to one alternative block for execution - diagram - have an arrow pointing to a diamond - put condition in a diamond - put each execution path in a box - draw arrows pointing from the condition diamond to the execution boxes - have the arrows come together to an empty circle - have an arrow pointing out of the circle - blocks can contain more than one statement - single statement: exactly one statement - compound statement: zero or more statements - multiple statements in a block must be surrounded with curly braces - otherwise compiler can't tell whether the second+ statements are part of the block - semicolons not necssary after right brace - can use braces even if block only has one statement - preferred by some programmers - easy to add temporary output - avoid dangling-else problem (discussed in 5.3) - placement of braces irrelevant to correct compilation - good style to do one of - if ( ) { ... } else { ... } - consistent with Java code conventions - if ( ) { ... } else { ... } - if ( ) { ... } else { ... } - consistency most important in your style choice - if-then statement - syntax if ( ) { } - call other version if-then-else statement - diagram - leave out second box and circle - not necessary because could simply do if-then-else with nothing in the else block - instantiable class definitions should not crash even if the client doesn't perform the appropriate tests - class itself should contain necessary tests Quick Check 1. a. valid b. invalid c. valid, but bad style d. valid, but bad style 2. a. if ( a < b) { x = y; } else { } b. if (a < b) { } else { } c. if ( a < b ) { x = y; } else { x = z; }