Chapter 3 - Java for Beginners Course

More If Statements

In the previous 2 sections we introduced the main types of if statements, however, they can be combined in more ways.

Multiple if-else blocks

If-then-else blocks can be combined in a compound statement that looks as follows:

if (1st boolean condition is true) {
	// then execute this block of code
} else if (2nd boolean condition is true) {
	// then execute this block of code
}
// ...
} else if (n-th boolean condition is true) {
	// then execute this block of code
} else {
	// execute this block of code if none of the conditions are true
}

Java will start evaluating the boolean conditions and will execute the block of code where the condition is true. In the case that more than one condition is true, only the block of code of the first one will be executed, meaning that the other ones will be ignored.

The last else statement is our default case, or put in a different way, it is the code to be executed if none of the given conditions were true. That last else statement is optional and doesn’t need to be provided if it isn’t required.

Remember that at most one block of code will be executed!
The conditions are evaluated in the order provided from top-to-bottom.

For example:

int age = 17;
double discount = 1.0d;

if (age >= 18) {
	discount = 0.0d;
	System.out.println("The age is 18 or over");
} else if (age >= 15) {
	discount = 0.2d;
	System.out.println("The age is between 15 and 17");
} else if (age >= 2) {
	discount = 0.5d;
	System.out.println("The age is between 2 and 14");
} else if (age >= 0) {
	discount = 1.0d;
	System.out.println("The age is between 0 and 1");
} else {
	discount = 0.0d;
	System.err.println("Incorrect age provided (negative value): " + age);
}

System.out.println("The discount to use is: " + discount);

Output:

The age is between 15 and 17
The discount to use is: 0.2

Analysing the example

In this case, the age variable has a value of 17. Java will start evaluating the conditions in the if statements to find the first one that evaluates to true, if any. In our case:

  1. First condition: age >= 18 evaluates to false, so the first block of code is not evaluated.

  2. Second condition: age >= 15 evaluates to true, meaning that our second block of code is executed, hence the message The age is between 15 and 17 is printed out and the discount variable is assigned a value of 0.2.

  3. The remaining conditions and the final else statement are ignored as Java already found a condition that is true and it already executed one of the blocks of code.

Why does the message say that the age is between 15 and 17 even though the condition only checks if the age is greater than or equal to 15?

You might have noticed that the message that we’re printing out states that The age is between 15 and 17, however, our 2nd condition only checks that (age >= 15).

The reason we can assert that the age is between 15 and 17 in our example case is that we know the following:

  1. We know that for Java to execute the 2nd block of code, the first condition must have been false. Otherwise, the code that Java would’ve executed would’ve been the first block of code (age >= 18).

  2. This means that if the 2nd block of code is executed, we know that the age must be less than 18 (age < 18).

  3. Our 2nd condition is checking that the age >= 15.

  4. Combining both facts, we know that our age >= 15 and that age < 18, meaning that the age must be between 15 and 17.

You can see that we’re applying the same reasoning to the other cases.

Nested if statements

Another way of combining if statements, is by nesting them, or in other words, having an if statement inside another one.

For example:

int age = 17;
double discount = 1.0d;

if (age >= 0) {
	if (age < 18) {
		discount = 1.0d;
		System.out.println("The age is between 0 and 17");
	} else {
		discount = 0.0d;
		System.out.println("The age is 18 or over");
	}
} else {
	discount = 0.0d;
	System.err.println("Incorrect age provided (negative value): " + age);
}

System.out.println("The discount to use is: " + discount);

Output:

The age is between 0 and 17
The discount to use is: 1.0

Analysing the nested-if example

In this case, the age variable has a value of 17. The condition of the first (outer) if statement evaluates to true (age >= 0), hence we need to evaluate the nested if statement.

Here, our second condition (age < 18) also evaluates to true and as a result the message The age is between 0 and 17 is printed out and the discount variable is given a value of 1.0d.

Both else statements will not be evaluated as the conditions of their corresponding if statements are true.

In this example we are nesting an if-then-else inside another if-then-else, however, you can nest any control flow statement inside another. You can nest compound if statements, if-then, for loops, and others that we’ll see in later sections.
Here, we only have a 2-level nesting. You can nest multiple times though, for example, you could have a 3rd if statement inside of the 2nd one (3-level nesting).
Although not a topic we’re covering in this course, multiple levels of nesting and very complex if statements can indicate a Code Smell and, depending on the case, should be simplified to improve maintainability and readability of the code.

Single-line if statements

For completeness, it is important to mention that if statements with a single line inside their block of code don’t need to use braces. For example, these 2 are equivalent:

if (age > 0) {
	System.out.println("The age is positive.");
}

if (age > 0)
	System.out.println("The age is positive.");

This is true for other control flow statements that we’ll cover, and in general its use is discouraged. The use of the braces ({ …​ }) provides clarity to anyone reading the code and helps avoiding ambiguity problems.