LECTURE NOTES OCTOBER 6 2004 ASSIGNMENTS 10/8 CodeLab 5 10/18 Assignment 2 BOOLEAN EXPRESSIONS, CONT'D Why? - complex boolean expressions What? - operator precedence rules How? - ! is considered a unary operator - relational operators come after additive operators - equality operators come after relational operators - && and || come after equality operators - operators with the same precedence are evaluated left-to-right Why? - mistakenly using a boolean for arithmetic What? - *arithmetic exception* What? - type mismatch How? 80 <= x < 90 USES OF BOOLEAN EXPRESSIONS What? - *defensive programming* - perform tests to ensure robustness - e.g. performing tests in an instantiable class so that the client doesn't have to What? - *flag* IF STATEMENTS Why? - default control flow is *sequential execution* What? - *control statement* - *selection statement* - *branching statement* How? - if-then statement - no then - only one statement in the block What? - diagram How? - diamonds for conditions - rectangles for code blocks - small circles where conditional flow becomes sequential again Why? - need more than one statement to be contingent upon the same condition How? - if statement - multiple statements in a block - requires curly braces - may use curly braces when only one statement (recommended) What? - placement of braces How? - consistency most important - if ( ) { ... } - if ( ) { ... } How? - diagram if statement with multiple statements in a block Why? - want to present an alternative What? - if-then-else How? - then block and else block - 0 or more statements in either block - brace placement - if ( ) { ... } else { ... } - consistent with Java code conventions - if ( ) { ... } else { ... } - if ( ) { ... } else { ... } - diagram