Java Conditional Statements: if, if…else, switch

After operators, the next level up is to use conditional statements, and Java supports the same conditional statements as JavaScript: if, if…else, and switch.

The if statement enables 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 that you had two double variables, assets and debts, and you wanted to compare the two to make sure that you're solvent. You might want the code to display a message, such as You're solvent. You can do that like this (note the before the ' in this code; you must add the to let Java know that the ' is not a quotation mark):

public class app
{
    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 will display the message; here are the results:

%java app
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 will be displayed if the amount in assets is less than or equal to the amount in debts:

public class app
{
    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, where I'm handling the cases in which the amount in assets is either the same as or greater than that in debts:

public class app
{
    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 also 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 catch: You can't use it with most of the many variable types Java defines. The only values that you can check in switch statements are byte, char, short, or int values. Here's an example where I'm working with integers:

public class app
{
    public static void main(String[] args)
    {
        int day = 5;

        switch(day) {
            case 0:
                System.out.println("Today is Sunday.");
                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, where I've converted the earlier if…else example to use the ?: operator:

public class app
{
    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);
    }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.255.189