ELA JAVA NOTES
CONTENTS
Control Flow Statements |
The if/else Statements |
Control Flow Statements
Without control flow statements, the interpreter executes these statements in the
order they appear in the file from left to right, top to bottom. The following
if statement conditionally executes the System.out.println statement, based on the
return value of Character.isUpperCase(aChar):
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar + " is upper case.");
}
Java provides control flow statements listed in the following table.
Statement Type Keyword
looping while, do-while , for
decision making if-else, switch-case
exception handling try-catch-finally, throw
branching break, continue, label:, return
In the sections that follow, you will see the following notation to describe the general form of a control flow statement:
control flow statement details
{
statement(s)
}
Technically, the braces, { and }, are not required if the block contains only one statement. However, we recommend that you always use { and }, because the code is easier to read and it helps to prevent errors when modifying code.
The while and do-while Statements
The for Statement
The if/else Statements
The switch Statement
Exception Handling Statements
Branching Statements
Summary of Control Flow Statements
Questions and Exercises: Control Flow
The if / else Statements
The if statement enables your program to selectively execute other statements,
based on criteria. For example, suppose that your program prints debugging
information, based on the value of a boolean variable named DEBUG. If DEBUG is
true, your program prints debugging information, such as the value of a variable,
such as x. Otherwise, your program proceeds 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 block governed by the
if is executed if a condition is true. Generally, the simple form of if can be
written like this:
if (expression) {
statement(s)
}
What if you want to perform a different set of statements if the expression is false?
You use the else statement for that. Consider another example. Suppose that your program
needs to perform different actions depending on whether the user clicks the OK button
or another button in an alert window. Your program could do this by using an if statement
along with an else 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
}
The else block is executed if the if part is false. Another form of the else
statement, else if, executes a statement based on another expression. An if
statement can have any number of companion else if statements but only one else.
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:
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);
}
}
The output from this program is:
Grade = C
You may have noticed that the value of testscore can satisfy more than one of the
expressions in the compound if statement: 76 >= 70 and 76 >= 60. However, as the
runtime system processes a compound if statement such as this one, once a condition
is satisfied, the appropriate statements are executed (grade = 'C';), and control
passes out of the if statement without evaluating the remaining conditions.
The Java programming language supports an operator, ?:, that is a compact version
of an if statement. Recall this statement from the MaxVariablesDemo program:
if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar + " is upper case.");
} else {
System.out.println("The character " + aChar + " is lower case.");
}
Here's how you could rewrite that statement using the ?: operator:
System.out.println("The character " + aChar + " is " +
(Character.isUpperCase(aChar) ? "upper" : "lower") +
"case.");
The ?: operator returns the string "upper" if the isUpperCase 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 the if statement
is secondary to the call to the println method. Once you get used to this construct,
it also makes the code easier to read.