How to Use if Statements in Java
In its most basic form, an if statement executes a single statement or a block of statements if a boolean expression evaluates to true. Here’s the syntax:
if (boolean-expression)
statement
The boolean expression must be enclosed in parentheses. If you use only a single statement, it must end with a semicolon. However, the statement can also be a statement block enclosed by braces. In that case, each statement within the block needs a semicolon, but the block itself doesn’t.
Here’s an example:
double commissionRate = 0.0;
if (salesTotal > 10000.0)
commissionRate = 0.05;
In this example, a variable named commissionRate is initialized to 0.0 and then set to 0.05 if salesTotal is greater than 10000.0.
Here’s an example that uses a block rather than a single statement:
double commissionRate = 0.0;
if (salesTotal > 10000.0)
{
commissionRate = 0.05;
commission = salesTotal * commissionRate;
}
In this example, the two statements within the braces are executed if salesTotal is greater than $10,000. Otherwise, neither statement is executed.
An if statement can include an else clause that executes a statement or block if the boolean expression is not true. Its basic format is
if (boolean-expression)
statement
else
statement
Here’s an example:
double commissionRate;
if (salesTotal <= 10000.0)
commissionRate = 0.02;
else
commissionRate = 0.05;
In this example, the commission rate is set to 2% if the sales total is less than or equal to $10,000. If the sales total is greater than $10,000, the commission rate is set to 5%.
Here’s an if statement with an else clause that uses a block instead of a single statement:
double commissionRate;
if (salesTotal <= 10000.0)
{
commissionRate = 0.02;
level1Count++;
}
else
{
commissionRate = 0.05;
level2Count++;
}
The statement that goes in the if or else part of an if-else statement can be any kind of Java statement, including another if or if-else statement. This arrangement is nesting, and an if or if-else statement that includes another if or if-else statement is a nested if statement.
The general form of a nested if statement is this:
if (expression-1)
if (expression-2)
statement-1
else
statement-2
else
if (expression-3)
statement-3
else
statement-4
In this example, expression-1 is the first to be evaluated. If it evaluates to true, expression-2 is evaluated. If that expression is true, statement-1 is executed; otherwise, statement-2 is executed. But if expression-1 is false, expression-3 is evaluated. If expression-3 is true, statement-3 is executed; otherwise, statement-4 is executed.
Here’s an example that implements a complicated commission structure based on two variables, named salesClass and salesTotal:
if (salesClass == 1)
if (salesTotal < 10000.0)
commissionRate = 0.02;
else
commissionRate = 0.04;
else
if (salesTotal < 10000.0)
commissionRate = 0.025;
else
commissionRate = 0.05;
The trick of using nested if statements is knowing how Java pairs else keywords with if statements. The rule is actually very simple: Each else keyword is matched with the most previous if statement that hasn’t already been paired with an else keyword.









