1. Technology

The if-then and if-then-else statements

By

Businesswoman sitting at desk working on laptop
Thomas Barwick/Stone/Getty Images

The if-then and if-then-else conditional statements let a Java program make simple decisions about what to do next. They work in the same logical way as we do when making decisions in real life. For example, when making a plan with a friend, I could say "If Mike gets home before 5.00pm then we'll go out for an early dinner." When 5.00pm arrives, the condition (i.e., Mike is home), which determines whether we all go out for an early dinner, will either be true or false. It's works exactly the same in Java.

The if-then Statement 

Let's say part of a program we're writing needs to calculate if the purchaser of a ticket is eligible for a child's discount. Anyone under the age of 16 gets a 10% discount on the ticket price. We can let our program make this decision by using an if-then statement:

if (age < 16)
    isChild = true;

In our program an integer variable called age holds the age of the ticket purchaser. The condition (i.e., is the ticket purchaser under 16) is placed inside the brackets. If this condition is true then the statement beneath the if statement is executed - in this case a boolean variable isChild is set to true.

The syntax follows the same pattern every time. The if keyword followed by a condition in brackets, with the statement to execute underneath:

if (condition is true) 
    execute this statement

The key thing to remember is the condition must equate to a boolean value (i.e., true or false).

Often a Java program needs to execute more than one statement if a condition is true. This is achieved by using a block (i.e., enclosing the statements in curly brackets):

if (age < 16)
{
    isChild = true;
    discount = 10;
}

This form of the if-then statement is the most commonly used, and I'd recommend using curly brackets even when there is only one statement to execute. It improves the readability of the code and leads to fewer programming mistakes. Without the curly brackets, it's easy to overlook the effect of the decision being made or to come back later and add another statement to execute but forget to also add the curly brackets.

The if-then-else Statement

The if-then statement can be extended to have statements that are executed when the condition is false. The if-then-else statement executes the first set of statements if the condition is true, otherwise the second set of statements are executed:

if (condition) 
{
    execute statement(s) if condition is true
}
else
{
    execute statement(s) if condition is false
}

In the ticket program let's say we need to make sure the discount is equal to 0 if the ticket purchaser is not a child:

if (age < 16)
{
    isChild = true;
    discount = 10;
}
else
{
    discount = 0;
}

The if-then-else statement also allows the nesting of if-then statements. This allows decisions to follow a path of conditions. For example, the ticket program might have several discounts. We might first test to see if the ticket purchaser is a child, then if they're a pensioner, then if they're a student and so on:

if (age < 16)
{
    isChild = true;
    discount = 10;
}
else if (age > 65)
{
    isPensioner = true;
    discount = 15;
}
else if (isStudent == true)
{
    discount = 5;
}

As you can see the if-then-else statement pattern just repeats itself. If at anytime the condition is true then the relevant statements are executed and any conditions beneath are not tested to see whether they are true or false. For example, if the age of the ticket purchaser is 67 then the highlighted statements are executed and the (isStudent == true) condition is never tested, the program just continues on.

There is something worth noting about the (isStudent == true) condition. I've written the condition to make it clear that we're testing whether isStudent has a value of true. But because it is a boolean variable we can actually write:


else if (isStudent)
{
    discount = 5;
}

If this is confusing, the way to think about it is like this - we know a condition is tested to be true or false. For integer variables like age, we have to write an expression that can be evaluated to true or false (e.g., age == 12, age > 35, etc..). But boolean variables already evaluate to be true or false. We don't need to write an expression to prove it because if (isStudent) is already saying "if isStudent is true..". If you wanted to test that a boolean variable is false just use the unary operator !. It inverts a boolean value, therefore if (!isStudent) is essentially saying "if isStudent is false..".

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Control Flow
  6. If-Then and If-Then-Else Conditional Statements in Java

©2014 About.com. All rights reserved.