LECTURE NOTES OCTOBER 8 2004 ASSIGNMENTS Today 11:58 PM CodeLab #5 Mon 10/18 Assignment 2 USES OF IF STATEMENTS What? - comparing a variable for being true or false How? - use identifier directly as the condition - choose identifier carefully so meaning clear NESTED-IF STATEMENTS Why? - need for a condition depends on a condition - testing conditions in a particular order What? - nested-if statement What? - writing the same thing in multiple ways How? - choose the simplest and clearest What? - indentation Why? - not required by compiler - clearer for readers - important for good programming style How? - one tab for each level of nesting Why? - long series of else-ifs can get really indented What? - else-if indentation How? if ... ... else if ... ... ... else if ... ... else ... What? - *dangling else problem* SWITCH STATEMENT Why? - selection control flow How? switch ( ) { : ... : } What? - arithmetic expression How? - data type must be char, byte, short, int - value is compared against each constant value of the case labels - executes body if a match exists - if no matching case, continues after switch statement What? - case labels How? case - constant: either named or literal - no two cases can have the same constant value - cases can be listed in any order default - reserved word - always executed if there is no matching case - at most one - safer to always include - recommend always place last What? - case body How? - sequence of zero or more statements - no left and right braces - individual cases don't have to include a statement