CSC153 2004S, Class 32: Conditionals in Java Admin: * This week: Conclude Java language issues (I hope) * Read "Loops in Java" for Wednesday * I hope you had a great break * Add the following line to the end of your .bashrc (e.g., with "gedit .bashrc") export PATH="$PATH:/usr/java/j2sdk1.4.1_03/bin" Overview: * Review of Java to date * Boolean expressions * Boolean methods * Simple conditionals: if statements * Complex conditionals: cond statements * Lab What do you know about Java? * An object-oriented programming language * Compile to "computer-independent" language which runs on a "virtual machine" * Running a Java program on our computers requires setting the PATH variable * Programs in Java look a lot like programs in C (similar syntax) * Need a main method which has the signature public static void main(String[] args) { ... } * Objects are described with classes, with three key parts * Fields (data) * Constructors (how to build them) * Methods (things you can do with built objects) * You know how to assign values, to write methods, and to call methods. * You need to know * How to test for conditions (debatable) * How to repeat things [Wed] * How to deal with errors in Java (Exceptions) [Thu] Conditionals are based on "Boolean" expressions: Values true or false * boolean type * values: true and false * Compare two values with <, <=, >, >=, ==, != * Join two boolean expressions with && (and), || (or) * Invert boolean exp with ! (a < b) && (b < c) * Observation: Infix operators, not prefix * In Java, boolean is not the same as integer while (1) NOT ALLOWED while (true) ALLOWED * Java is your nanny Boolean expressions are most typically used in if statements if (BOOL_EXP) { STUFF-TO-DO-IF-EXP-HOLDS } else { STUFF-TO-DO-IF-EXP-DOESNTHOLD } * The else part is optional * The parens are probably optional * The braces are optional if you have only one statement in the body There is no COND! if (BOOL_EXP) { STUFF-TO-DO-IF-EXP-HOLDS } else if (BOOL_EXP) { ETC } else if (BOOL_EXP) { ETC } else { ETC } if (BOOL_EXP) { STUFF-TO-DO-IF-EXP-HOLDS } else if (BOOL_EXP) { ETC } else if (BOOL_EXP) { ETC } else { ETC } Java also has switch statements switch (EXP) { case VAL1: CODE case VAL2: CODE case VAL3: CODE } * Compute the value of EXP * Find the first matching value * Execute code following the corresponding case. * Keep executing until you find a break or the end of the switch. switch (val) of { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); } * If val is 1, "One\nTwo\nTree\n" gets printed * Why is case better than nested ifs? * Perhaps more readable * Less typing of (val == VAL), but extra breaks * Faster * In a naive implementation, a case statement is just a series of nested ifs O(n) * In a moderately smart implementation, you can have a "binary search tree" of possible values O(logn) * In a really smart implementation, this is just an indexed goto O(1) * Sometimes the "fall through" behavior is useful * However, generally it breaks when programmers forget their breaks. Do the lab! Programming puzzle Why write the test as 1 == this.month rather than this.month == 1 ? * C programming trick: If you accidentally forget an = sign, the first will lead to an error, the second won't (except in nannyish Java)