![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Java's ifstatement 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
. IfDEBUG
were set totrue
, then your program would print debugging information such as the value of some variable likex
. Otherwise, your program would proceed normally. A segment of code to implement this might look like this:This is the simplest version of theif (DEBUG) { System.out.println("DEBUG: x = " + x); }if
statement: the statement governed by theif
is executed if some condition is true. Generally, the simple form ofif
can be written like this:What if you wanted to perform a different set of statements if the expression is false? You use the elseif (expression) { statement }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:The. . . // 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 . . . }else
block is executed if theif
part is false. Another form of theelse
statement,else if
, executes a statement based on another expression. Anif
statement can have any number of companionelse if
statements, but only oneelse
. Following is a program,IfElseDemo
, that assigns a grade 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:
The output from this program is:public class IfElseDemo { public static void main(String[] args) { int testscore = 76; 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'; } System.out.println("Grade = " + grade); } }You may have noticed that some values ofGrade = Ctestscore
could satisfy more than one of the expressions in the compoundif
statement. For instance, a score of 76 would evaluate totrue
for two of the expressions in theif
statement:testscore >= 70
andtestscore >= 60
. However, as the runtime system processes a compoundif
statement such as this one, once a condition is satisfied (76 >= 70), the appropriate statements are executed (grade = 'C';
), and control passes out of theif
statement without evaluating the remaining conditions.The Java programming language supports an operator,
?:
, that is a compact version anif
statement. Recall this statement from theMaxVariablesDemo
program:
Here's how you could rewrite that statement using the ?: operator:if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); }TheSystem.out.println("The character " + aChar + " is " + Character.isUpperCase(aChar) ? "upper" : "lower" + "case.");?:
operator returns the string"upper"
if theisUpperCase
method returns true. Otherwise, it returns the string"lower"
. The result is concatenated with other parts of a message to be displayed. Using?:
makes sense here because theif
statement is secondary to the call to theprintln
method. Once you get used to this construct, it also makes the code easier to read.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |