If you look at our Apex code, you’ll notice that it’s been quite linear so far. There’s always only one possible outcome for the program. However, in the real world, this is rarely the case! There is always some sort of conditional logic. For example, if an opportunity is important, make a call. Otherwise, just send an email. In this article, we’ll discuss how to implement this type of logic in Apex.
“If/else in Apex” topics fall into the Control Structure category because they control the way your code runs. The second type of control structure is loops, but those are a huge topic that we’ll discuss in future articles.
Syntax
The complete syntax for “if/else” looks like this:
if(condition) {
// code
} else if(condition) {
// code
} else {
// code
}
But let’s start small with just the first element.
The “if” statement
If specifies a block of code that will be executed when the condition is true. Here’s an example that you can copy into your anonymous window:
if(5 > 2) {
System.debug('Five is greater than two. Wow!');
}
If we run this code, we’ll see “Five is greater than two. Wow!
” in the logs. If the condition in “if” is false, the block won’t be executed. For example, no “Five is greater than two. Wow!
” will be printed this time:
if(5 > 10) {
System.debug('Five is greater than ten. Wow!');
}
Of course, we rarely have that type of condition. Usually, we use variables instead:
Integer age = 15;
Integer legalAge = 21;
if(age < legalAge) {
System.debug('You are not allowed into the bar!');
}
// code after
If the person is not of legal age, the message won’t be printed into the logs.

The “if/else in Apex” statement
We can add “else” as the next statement after “if” to specify what code to run if the first condition is wrong:
Integer age = 18;
if(age > 21) {
System.debug('Welcome to the bar!');
} else {
System.debug('Sorry, you are not allowed.');
}
18 is definitely less than 21, so we will see a sad “Sorry, you are not allowed” message in the logs. Here’s how it works in general:

The “If/else if” and “else” statements
What if we want to check a few more cases instead of just one? Let’s say we want to sort a person’s age into three categories: young, adult and old. With only “if” and “else,” it would take a lot of coding—and the code would be awful.
We have “if..else if..” precisely for such tasks. Here’s the syntax:
Integer age = 21;
if(age < 18) {
System.debug('young');
} else if(age > 18 && age < 65) {
System.debug('adult');
} else {
System.debug('old');
}
// block after if
Output
adult
In contrast to just the “else” statement, we have to specify a condition when the code within the “if/else” block will be executed. In this case, the age > 18 && age < 65
condition will evaluate to true, and we’ll jump into the “adult” code block.
Multiple if/else if
We can have as many else if statements as we want. But what happens if we have multiple statements where a condition can evaluate to true? Take a look at the example below. What will be printed out?
Integer myNumber = 35;
if(myNumber > 50) {
System.debug('Number is greater than 50');
} else if(myNumber > 40) {
System.debug('Number is greater than 40');
} else if(myNumber > 30) {
System.debug('Number is greater than 30');
} else if(myNumber > 20) {
System.debug('Number is greater than 20');
}
// block after if
// block after if
The first two conditions will not be printed out because 32 is not greater than 50 or 40. However, both myNumber > 30 and myNumber > 40 could evaluate to true.
The answer is that only myNumber > 30 will be evaluated and the message “Number is greater than 30” will be printed to the logs. Why? Because Apex only enters the first else if statement that evaluates to true. After that, it doesn’t evaluate any other conditions and will execute other code after the “if” block.

Nested “if” statements
We can have “if” statements within an “if” statement. It’s basically endless! However, just because we can do this doesn’t mean we should. Good code typically doesn’t have more than three levels of nested “if” statements. Let’s take a look at how it works:
Integer age = 15;
String name = 'Alex';
if(age < 24) {
if(name == 'Alex') {
System.debug('Wow, it's Alex!');
} else {
System.debug('Sorry, you are not Alex and you are not 24!');
}
}
Almost all the time, we can simplify this code to just a one-level “if” statement:
Integer age = 15;
String name = 'Alex';
if(age < 24 && name == 'Alex') {
System.debug('Wow, it's Alex!');
} else if(age < 24) {
System.debug('Sorry, you are not Alex and you are not 24!');
}
What is a condition?
We’ve yet to discuss what exactly conditions are. A condition is basically anything that becomes true/false. We can also say that it “evaluates” to true or false. It can be a Boolean or any result of an operation. For example, both examples here are equivalent:
String name = 'Alex';
if(name == 'Alex') { // check the condition inside of if statement
// code
}
Boolean isAlex = name == 'Alex'; // check condition outside of if statement.
if(isAlex) {
// code
}
You can also use operations within “if” such as addition or calling methods. In theory, you can use any operation. In practice, however, we usually only use Boolean operations here.
String name = 'Alex';
Integer age = 15;
Boolean isAlex = name == 'Alex';
Boolean oldEnough = age > 21;
if(isAlex || oldEnough) {
System.debug('You are welcome if you are old enough or if you are Alex!');
}
Quite often, you’ll see a method call in the condition that will return a Boolean value. It’s quite common and considered a best practice.
String name = '';
if(name.length() == 0) {
System.debug('The name is empty');
}
Links
- Nice quick explanation on if/else (SFDC99).
- Using Conditional Logic in Apex (Trailhead).
- Control Structures in Apex (Salesforce Developer Documentation).
- Why you shouldn’t nest your code (YouTube).