CONDITIONALS ============ An "if" statement uses a boolean expression to decide whether to execute a set of statements. The form is if (boolValue) { statements; } The statements are executed if and only if "boolValue" is "true". The parentheses around the boolean expression are required (for no good reason). boolean pass = score >= 75; if (pass) { output("You pass CS 61B"); } else { // The following line executes if and only if score < 75. output("You are such an unbelievable loser"); } if-then-else clauses can be (1) nested and (2) daisy-chained. Nesting allows you to build decision trees. Daisy-chaining allows you to present more than two alternatives. For instance, suppose you want to find the maximum of three numbers. if (x > y) { if (x > z) { maximum = x; } else { maximum = z; } } else if (y > z) { maximum = y; } else { maximum = z; } Some long chains of if-then-else clauses can be simplified by using a "switch" statement. "switch" is appropriate only if every condition tests whether a variable x is equal to some constant. switch (month) { | if (month == 2) { case 2: | days = 28; days = 28; | } else if ((month == 4) || (month == 6) || break; | (month == 9) || (month == 11)) { case 4: | days = 30; case 6: | } else { case 9: | days = 31; case 11: | } days = 30; | break; default: days = 31; break; } // These two code fragments do exactly the same thing. IMPORTANT: "break" jumps to the end of the "switch" statement. If you forget a break statement, the flow of execution will continue right through past the next "case" clause, which is why cases 4, 6, and 9 work right. If month == 12 in the following example, both Strings are printed. switch (month) { case 12: output("It's December."); // Just keep moving right on through. case 1: case 2: case 11: output("It's cold."); } However, this is considered bad style, because it's hard to read and understand. If there's any chance that other people will need to read or modify your code (which is the norm when you program for a business), don't code it like this. Use break statements in the switch, and use subroutines to reuse code and clarify the control flow. Observe that the last example doesn't have a "default:" case. If "month" is not 1 nor 2 nor 11 nor 12, Java jumps right to the end of the "switch" statement (just past the closing brace) and continues execution from there. THE "return" KEYWORD ==================== Like conditionals, "return" affects the flow of control of a program. It causes a method to end immediately, so that control returns to the calling method. Here's a recursive method that prints the numbers from 1 to x. public static void oneToX(int x) { if (x < 1) { return; } oneToX(x - 1); System.out.println(x); } The return keyword serves a dual purpose: it is also the means by which a function returns a value. A _function_ is a method that is declared to return a non-void type. For instance, here's a function that returns an int. public int daysInMonth(int month) { switch (month) { case 2: return 28; case 4: case 6: case 9: case 11: return 30; default: return 31; } } The "return" value can be an expression. Some examples: return x + y - z; return car.velocity(time);