Java Conditional Statements: if , if else , switchAfter operators, the next level up is to use conditional statements. Java supports the same conditional statements as JavaScript: if , if...else , and switch . The if statement allows you to check a condition, which you create using the Java conditional operators, such as < , > , and == : if ( condition ) { code executed if condition is true } else { code executed if condition is false } For example, say you had two double variables , assets and debts , and you wanted to compare the two to make sure you're solvent. You might want the code to display a message, such as "You're solvent." . You can do that like this: Listing ch10_07.javapublic class ch10_07 { public static void main(String[] args) { double assets = 175.99; double debts = 115.99; if (assets > debts) { System.out.println("You're solvent."); } } } As we've seen when discussing JavaScript, the if statement checks its condition and, if that condition evaluates to true , executes the code in the if statement's body. In this case, the code displays the message; here are the results: %java ch10_07 You're solvent. You can also explicitly handle the case in which the condition in an if statement turns out to be false by including an else clause. If the condition in the if statement evaluates to false , the code in the else clause is executed if the if statement has such a clause. Here's an example. In this case, the second message is displayed if the amount in assets is less than or equal to the amount in debts : public class ch10_07 { public static void main(String[] args) { double assets = 175.99; double debts = 115.99; if (assets > debts) { System.out.println("You're solvent."); } else { System.out.println("Uh oh."); } } } You can also create "ladders" of if..else statements like this; I'm handling the cases in which the amount in assets is either the same or greater than that in debts : public class ch10_07 { public static void main(String[] args) { double assets = 175.99; double debts = 115.99; if (assets > debts) { System.out.println("You're solvent."); } else { if(assets == debts) { System.out.println("You're broke."); } else { System.out.println("Uh oh."); } } } } As with JavaScript, Java supports a switch statement: switch( test ){ case value1: . . . code executed if test matches value1 . . . break; case value2: . . . code executed if test matches value2 . . . break; default: . . . code executed if test doesn't matches any case . . . break; } But there's a catchyou can't use it with most of the many variable types that Java defines. The only values you can check in switch statements are byte , char , short , and int values. Here's an example in which I'm working with integers: Listing ch10_08.javapublic class ch10_08 { public static void main(String[] args) { int day = 5; switch(day) { case 0: System.out.println("Today is Monday."); break; case 1: System.out.println("Today is Monday."); break; case 2: System.out.println("Today is Tuesday."); break; case 3: System.out.println("Today is Wednesday."); break; case 4: System.out.println("Today is Thursday."); break; case 5: System.out.println("Today is Friday."); break; default: System.out.println("It must be Saturday."); } } } There's another useful way of handling if...else situations: You can use the Java ?: operator. This operator returns one of two values depending on whether an expression evaluates to true or false . You put the condition in front of the ? , the value that this operator should return if the condition is true immediately after the ? , and the value that the operator should return if the condition is false after the colon . Here's an example in which I've converted the earlier if...else example to use the ?: operator: Listing ch10_09.java public class ch10_09 { public static void main(String[] args) { double assets = 175.99; double debts = 115.99; String output; output = assets > debts ? "You're solvent." : "Uh oh."; System.out.println(output); } } |