Apex Developer Guide
Release Notes
Conditional (If-Else) Statements
Switch Statements
Apex Reference
Previous PDF Versions
DID THIS ARTICLE SOLVE YOUR ISSUE?
Let us know so we can improve!
Let us know so we can improve!
The conditional statement in Apex works similarly to Java.
1if ([Boolean_condition])
2 // Statement 1
3else
4 // Statement 2The 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}