Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 8: Flow control with if and else

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

The Java Lesson 8

Flow control with if and else

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 >= 1Cool {
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:

  1. The program prompts for and reads a person's age from the user.

  2. 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 < 1Cool {
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 < 1Cool {
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 < 1Cool {
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(" " + "C - charge");
System.out.println(" " + "P - payment");
System.out.println(" " + "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:

  1. 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.

  2. 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.

  3. 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).

  4. 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.

  5. 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.

Review questions

  1. 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");

  1. the statements will not compile

  2. Big number

  3. Small number

  4. You win
    Big number

  5. You win
    Small number

  1. 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'");

  1. the statements will not compile

  2. Now it is 'a'

  3. Now it is 'b'

  4. Now it is 'a'
    Now it is 'b'

  5. nothing will display

  1. 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");
    }

  1. the statements will not compile

  2. fee
    foe

  3. fie
    foe

  4. fum

  5. fee
    fie
    foe
    fum

  1. 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");

  1. the statements will not compile

  2. Doc

  3. Dopey

  4. Doc
    Dopey

  5. nothing will display


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy