Overview
The if and else statements allow a block of code or a single statement to be either executed or bypassed depending upon the result of a comparison operation. They are an essential part of Java "flow control" by which execution may proceed along alternate logic paths within a program.
The if statement
Identifies a single statement or a block of statements to be executed if an expression evaluates to true
Has two forms. The general syntax is either of the following:
if (expression) statement;
if (expression) {
statements;
}The first form is often referred to as a "single statement if". For example,
if (amountDue > 0)
System.out.println("You owe " + amountDue);will display the message only if the value of amountDue is greater than zero. The logic path merges at the next statement in the program.
The second form is often referred to as a "multiple statement if". For example,
if (amountDue > 0) {
totalDueFromAllCustomers += amountDue;
System.out.println("You owe " + amountDue);
}will add to the grand total and display the message only if the value of amountDue is greater than zero. The logic path merges at the first statement after the end of the statement block.
Can lure unsuspecting programmers into some nasty errors. For example,
if (amountDue > 0);
System.out.println("You owe " + amountDue);will display the message no matter what value amountDue has. The accidental semicolon after the comparison expression is to blame. The code says that if the expression is true you want to do nothing. Logic then merges at the next statement, so the message is always displayed.
Another common error is to forget the braces in what was intended to be a multiple statement if. For example,
if (amountDue > 0)
totalDueFromAllCustomers += amountDue;
System.out.println("You owe " + amountDue);will add to the grand total if the value of amountDue is greater than zero. The message, however, will always be displayed. Omitting the braces makes this a single statement if, so the logic path merges at the statement that displays the message.
The else statement
Defines an alternative and mutually exclusive logic path to be followed if the preceding if statement expression evaluates to false
Must always be paired with, and follow, an if statement. It can never be coded separately.
Has two forms. The general syntax is either of the following:
else statement;
else {
statements;
}The first form is often referred to as a "single statement else". For example,
if (amountDue > 0)
System.out.println("You owe " + amountDue);
else
System.out.println("You owe nothing");will display how much is owed if amountDue is greater than zero. Otherwise, the "You owe nothing" message will be displayed. The two logic paths are mutually exclusive and merge at the next statement in the program.
The second form is often referred to as a "multiple statement else". For example,
if (amountDue > 0) {
totalDueFromAllCustomers += amountDue;
System.out.println("You owe " + amountDue);
}
else {
customerRating = 'A';
System.out.println("You owe nothing");
}will add to the grand total and display how much is owed if amountDue is greater than zero. Otherwise, the customerRating will be set to 'A' and the "You owe nothing" message will be displayed. The two logic paths are mutually exclusive and merge at the first statement after the end of the else block.
Can trap programmers who get sloppy. For example,
if (age < 65)
System.out.println("Regular admission");
else;
System.out.println("Senior admission");will always display the "Senior admission" message no matter what value age has. The accidental semicolon after the else says that if the expression is false you want to do nothing. Logic then merges at the next statement, so the message is always displayed.
Nesting
Is permitted. An if or an if-else can be coded within an if or an if-else.
Example:
public class App {
public static void main(String[] args) {
int age;
System.out.print("Enter age: ");
age = Keyboard.readInt();
if (age >= 18) {
if (age < 65) {
System.out.println("$5 admission");
}
else {
System.out.println("$4 admission");
}
}
else {
if (age < 5) {
System.out.println("Free admission");
}
else {
System.out.println("$2 admission");
}
}
}
}Notes:
The program prompts for and reads a person's age from the user.
Based upon the value of age, it displays a different admission fee (under 5 is free, 5-17 is 2 dollars, 18-64 is 5 dollars, and 65 and over is 4 dollars).
Can lead to nasty logic errors. The following code, for example, doesn't do what you initially expect.
if (age < 65)
if (age < 18) {
System.out.print("Child discount");
}
else {
System.out.print("Senior discount");
}It appears that for those 65 and over the "Senior discount" message will display, for those under 18 the "Child discount" message will display, and for all others no message will display.
What actually displays is nothing for those 65 and over, "Child discount" for those under 18, and "Senior discount" for those from 18-64!!
Because an else is always paired with the last if that didn't have an else and to which it can legally belong, the preceding code is actually the same as the following:
if (age < 65)
if (age < 18) {
System.out.print("Child discount");
}
else {
System.out.print("Senior discount");
}This error can easily be avoided by ALWAYS using the multiple statement syntax for the if and else. For example, the inclusion of two braces in the original code fixes the error:
if (age < 65) {
if (age < 18) {
System.out.print("Child discount");
}
}
else {
System.out.print("Senior discount");
}
Example
The following program demonstrates nested if-else logic. It determines a customer's new balance based upon their starting balance, a transaction amount, and a transaction code entered by the user. A "charge" transaction will add to the customer's balance while "payment" and "return" transactions will subtract from it.
public class App {
public static void main(String[] args) {
// Variables
double balance;
char code;
double amount;
boolean isGood;
// Prompt for and read data
System.out.print("Enter customer's starting balance: ");
balance = Keyboard.readDouble();
System.out.print("Enter transaction amount: ");
amount = Keyboard.readDouble();
System.out.println("Transaction codes are");
System.out.println("\t" + "C - charge");
System.out.println("\t" + "P - payment");
System.out.println("\t" + "R - refund or return");
System.out.print("Enter transaction code: ");
code = Keyboard.readChar();
// Process based upon transaction code
if (code == 'C' || code == 'c') {
balance += amount;
isGood = true;
}
else {
if (code == 'P' || code == 'p') {
balance -= amount;
isGood = true;
}
else {
if (code == 'R' || code == 'r') {
balance -= amount;
isGood = true;
}
else {
isGood = false;
}
}
}
// Display result
if (isGood) {
System.out.println("New balance is " + Utility.moneyFormat(balance));
}
else {
System.out.println("Invalid transaction code");
}
}
}Notes:
The balance, code, and amount variables hold data entered by the user. The isGood variable is set during processing to indicate whether the transaction code is valid.
After all data has been read from the user, the transaction is processed based upon its transaction code. The comparison expressions allow for the possibility that the user may have entered the transaction code in either upper or lower case.
If the transaction code is valid, the balance is adjusted accordingly and isGood set to true (indicating a good transaction). If an invalid transaction code was entered, logic flows to the point where isGood is set to false (indicating a bad transaction).
After the transaction has been processed, the result is displayed based upon the value of isGood. For a successful transaction, the customer's new balance is displayed using the moneyFormat() method of my Utility class (for more information about this class and its methods, click here). If the transaction was bad, an error message is displayed.
Many Java programmers structure nested if-else code to produce what appears to be an else-if. For example, the above statements that process the transaction might be coded as follows:
if (code == 'C' || code == 'c') {
balance += amount;
isGood = true;
}
else if (code == 'P' || code == 'p') {
balance -= amount;
isGood = true;
}
else if (code == 'R' || code == 'r') {
balance -= amount;
isGood = true;
}
else {
isGood = false;
}If you try this code, you will see that it works. An easier way to simplify the code will be presented in the next lesson.
Lab exercise for Ferris students
E-mail your answers to this assignment no later than the due date listed in the class schedule.
Review questions
Assuming all unseen code is correct and that number is a byte variable with a value of 100, what will be displayed by the following statements?
if (number == 17);
System.out.println("You win");
if (number < 100)
System.out.println("Small number");
else
System.out.println("Big number");
the statements will not compile
Big number
Small number
You win
Big numberYou win
Small number
Assuming all unseen code is correct and that field is a char variable with a value of 'a', what will be displayed by the following statements?
if (field = 'b')
System.out.println("Now it is 'a'");
else
System.out.println("Now it is 'a'");
the statements will not compile
Now it is 'a'
Now it is 'b'
Now it is 'a'
Now it is 'b'nothing will display
Assuming all unseen code is correct, what will be displayed when an attempt is made to compile and execute the following statements?
int x = 3, y = 1, z =5;
if (x > y) {
if (z <= y) {
System.out.println("fee");
}
else {
System.out.println("fie");
}
System.out.println("foe");
}
else {
if (y > z)
System.out.println("fum");
}
the statements will not compile
fee
foefie
foefum
fee
fie
foe
fum
Assuming all unseen code is correct and that x is a boolean variable with a value of false, what will be displayed by the following statements?
if (x)
System.out.println("Doc");
System.out.println("Dopey");
the statements will not compile
Doc
Dopey
Doc
Dopeynothing will display