If-Then and If-Then-Else Conditional Statements in Java

What to Do Next

Woman sitting at desk working on laptop writing Java conditional statements

Thomas Barwick/Stone/ Getty Images

The

if-then
and
if-then-else
conditional statements let a Java program make simple decisions

For example, when making a plan with a friend, you could say "If Mike gets home before 5:00 PM, then we'll go out for an early dinner." When 5:00 PM arrives, the condition (i.e., Mike is home), which determines whether everyone goes out for an early dinner, will either be true or false. It 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
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
if (condition is true) 
execute this statement

The key thing to remember is the condition must equate to a

boolean

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

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
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
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 any time 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)

There is something worth noting about the

(isStudent == true)
condition. The condition is written to make it clear that we're testing whether
isStudent
has a value of true, but because it is a
boolean

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

However, 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 want to test that a boolean variable is false, just use the unary operator
!
. It inverts a boolean value, therefore
if (!isStudent)
Format
mla apa chicago
Your Citation
Leahy, Paul. "If-Then and If-Then-Else Conditional Statements in Java." ThoughtCo, Aug. 27, 2020, thoughtco.com/the-if-then-and-if-then-else-statements-2033884. Leahy, Paul. (2020, August 27). If-Then and If-Then-Else Conditional Statements in Java. Retrieved from https://www.thoughtco.com/the-if-then-and-if-then-else-statements-2033884 Leahy, Paul. "If-Then and If-Then-Else Conditional Statements in Java." ThoughtCo. https://www.thoughtco.com/the-if-then-and-if-then-else-statements-2033884 (accessed March 29, 2024).