Java Programming/Flow control/Conditional statements

From Wikibooks, open books for an open world
< Java Programming‎ | Flow control
Jump to: navigation, search

[edit] Conditional statements

Conditional statements allow a program to take a different path depending on some condition.

[edit] If

The if statement is a section of code that only executes if the associated boolean expression is true. The if statement may optionally be followed by an else statement which will execute if that boolean expression is false.

The structure of an if statement is as follows:

Computer code
if (<boolean expression>) {
    // Do something
} else {
    // Do something else
}

Additionally, if code is to be executed based on more conditions, an else-if statement may be used. else-if statements come after the if statement, but before the else statement.

The structure of an if-else-if statement is as follows:

Computer code
if (<boolean expression>) {
    // Do something
} else if (<another boolean expression>) {
    // Do another thing
} else {
    // Do something else
}

Here is an example to illustrate:

Computer code
int a = -5;
if (a > 0) {
    System.out.println("a is positive"); // a is not greater than 0, so this statement will not execute
} else if (a < 0) {
    System.out.println("a is negative"); // a IS less than 0, so this statement will execute
} else {
    System.out.println("a is zero"); // a does not equal 0, so this statement will not execute
}
Note If only one statement is to be executed after an if statement, it does not have to be enclosed in brackets. For example, if (i == 0) i = 1; is a perfectly valid portion of Java code. This works for most control structures, such as else and while.

[edit] Switch

The switch statement evaluates an integer (or enum, starting in J2SE 5.0; or String, starting in J2SE 7.0), and, based on the value provided, jumps to a specific case within the switch block and executes code until the break command is encountered or the end of the block. If the switch value does not match any of the case values, execution will jump to the optional default case.

The structure of a switch statement is as follows:

Computer code
switch (<integer or enum or String value>) {
    case <case value>:
        // Do something
        break;
    case <another case value>:
        // Do something else
        break;
    default:
        // Optional default handling
}

Here is an example to illustrate:

Computer code
int i = 3;
switch(i) {
    case 1:
        System.out.println("i equals 1"); // i doesn't equal 1, so this code won't execute
        break;
    case 2:
        System.out.println("i equals 2"); // i doesn't equal 2, so this code won't execute
        break;
    default:
        System.out.println("i equals something other than 1 or 2"); // i has not been handled so far, so this code will execute
}

If the break statement does not end a case, then it will fall through to the case below.

Look at this example to see how it's done:

Computer code
int i = -1;
switch(i) {
    case -1:
    case 1:
        System.out.println("i is 1 or -1"); // i is -1, so it will fall through to this case and execute this code
        break;
    case 0:
        System.out.println("i is 0"); // The break command is used before this case, so if i is 1 or -1, this will not execute
}

Starting in J2SE 5.0, the switch statement can also be used with an enum value instead of an integer.

Though enums have not been covered yet, here is an example so you can see how it's done (note that the enum constants in the cases do not need to be qualified with the type:

Computer code
Day day = Day.MONDAY; // Day is a fictional enum type containing the days of the week
switch(day) {
    case MONDAY:
        System.out.println("Mondays are the worst!"); // Since day == Day.MONDAY, this statement will execute
        break;
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
        System.out.println("Weekdays are so-so.");
        break;
    case FRIDAY:
    case SATURDAY:
    case SUNDAY:
        System.out.println("Weekends are the best!");
        break;
}

Starting in J2SE 7.0, the switch statement can also be used with an String value instead of an integer.

Computer code
String day = "Monday";
switch(day) {
    case "Monday":
        System.out.println("Mondays are the worst!"); // Since day == "Monday", this statement will execute
        break;
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
        System.out.println("Weekdays are so-so.");
        break;
    case "Friday":
    case "Saturday":
    case "Sunday":
        System.out.println("Weekends are the best!");
        break;
    default:
        throw new IllegalArgumentException("Invalid day of the week: " + day);
}