The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

The if-else Statement

Java's if-else statement provides your programs with the ability to selectively execute other statements based on some criteria. For example, suppose that your program printed debugging information based on the value of some boolean variable named DEBUG. If DEBUG were set to true, then your program would print debugging information such as the value of some variable like x. Otherwise, your program would proceed normally. A segment of code to implement this might look like this:
if (DEBUG) {
    System.out.println("DEBUG: x = " + x);
}
This is the simplest version of the if statement: the statement governed by the if is executed if some condition is true. Generally, the simple form of if can be written like this:
if (expression) {
    statement
}
So, what if you wanted to perform a different set of statements if the expression is false? Well, you can use the else statement for that. Consider another example. Suppose that your program needs to perform different actions depending on whether the user clicks on the OK button or the Cancel button in an alert window. Your program could do this using an if statement:
    . . .
    // response is either OK or CANCEL depending
    // on the button that the user pressed
    . . .
if (response == OK) {
    . . .
    // code to perform OK action
    . . .
} else {
    . . .
    // code to perform Cancel action
    . . .
}
This particular use of the else statement is the catch-all form. The else block is executed if the if part is false. There is another form of the else statement, else if which executes a statement based on another expression. For example, suppose that you wrote a program that assigned grades based on the value of a test score, an A for a score of 90% or above, a B for a score of 80% or above and so on. You could use an if statement with a series of companion else if statements, and an else to write this code:
int testscore;
char grade;

if (testscore >= 90) {
    grade = 'A';
} else if (testscore >= 80) {
    grade = 'B';
} else if (testscore >= 70) {
    grade = 'C';
} else if (testscore >= 60) {
    grade = 'D';
} else {
    grade = 'F';
}
An if statement can have any number of companion else if statements, but only one else. You may have noticed that some values of testscore could satisfy more than one of the expressions in the compound if statement. For instance, a score of 76 would evaluate to true for two of the expressions in the if statement: testscore >= 70 and testscore >= 60. However, as the runtime system processes a compound if statement such as this one, once a condition is satisfied (76 >= 70), the appropriate statements are executed (grade = 'C';), and control passes out of the if statement without evaluating the remaining conditions.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search