Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Conditional (If-Else) Statements

The conditional statement in Apex works similarly to Java.
1if ([Boolean_condition]) 
2    // Statement 1
3else
4    // Statement 2
The else portion is always optional, and always groups with the closest if. For example:
1Integer x, sign;
2// Your code
3if (x <= 0) if (x == 0) sign = 0; else sign = -1;
is equivalent to:
1Integer x, sign;
2// Your code
3if (x <= 0) {
4    if (x == 0) {
5           sign = 0; 
6    } else  {
7           sign = -1;
8    }
9}
Repeated else if statements are also allowed. For example:
1if (place == 1) {
2    medal_color = 'gold';
3} else if (place == 2) {
4    medal_color = 'silver';
5} else if (place == 3) {
6    medal_color = 'bronze';
7} else {
8    medal_color = null;
9}