For Loop:
Apex supports three variations of the for loop:
The traditional for loop:
The list or set iteration for loop:
where variable must be of the same primitive or sObject type as list_or_set.
The SOQL for loop:
Both variable and variable_list must be of the same sObject type as is returned by the soql_query.
If-Else:
The conditional statement in Apex works similarly to Java:
Syntax:
The else portion is always optional, and always groups with the closest if.
Example:
is equivalent to:
Repeated else if statements are also allowed.
For example:
While Loops:
The Apex while loop repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
Unlike do-while, the while loop checks the Boolean condition statement before the first loop is executed. Consequently, it is possible for the code block to never execute.
Do-While Loops:
The Apex do-while loop repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
As in Java, the Apex do-while loop does not check the Boolean condition statement until after the first loop is executed. Consequently, the code block always runs at least once. As an example, the following code outputs the numbers 1 - 10 into the debug log:
Apex supports three variations of the for loop:
The traditional for loop:
for (init_stmt; exit_condition; increment_stmt) { code_block }
The list or set iteration for loop:
for (variable : list_or_set) { code_block }
where variable must be of the same primitive or sObject type as list_or_set.
The SOQL for loop:
for (variable : [soql_query]) { code_block } or for (variable_list : [soql_query]) { code_block }
Both variable and variable_list must be of the same sObject type as is returned by the soql_query.
If-Else:
The conditional statement in Apex works similarly to Java:
Syntax:
if ([Boolean_condition]) { Statement 1 } else { Statement 2 }
The else portion is always optional, and always groups with the closest if.
Example:
Integer x, sign; // Your code if (x <= 0) if (x == 0) sign = 0; else sign = -1;
is equivalent to:
Integer x, sign; // Your code if (x <= 0) { if (x == 0) { sign = 0; } else { sign = -1; } }
Repeated else if statements are also allowed.
For example:
if (place == 1) { medal_color = 'gold'; } else if (place == 2) { medal_color = 'silver'; } else if (place == 3) { medal_color = 'bronze'; } else { medal_color = null; }
While Loops:
The Apex while loop repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
while (condition) { code_block }
Unlike do-while, the while loop checks the Boolean condition statement before the first loop is executed. Consequently, it is possible for the code block to never execute.
Integer count = 1; while (count < 11) { System.debug(count); count++; }
Do-While Loops:
The Apex do-while loop repeatedly executes a block of code as long as a particular Boolean condition remains true.
Syntax:
do { code_block } while (condition);
As in Java, the Apex do-while loop does not check the Boolean condition statement until after the first loop is executed. Consequently, the code block always runs at least once. As an example, the following code outputs the numbers 1 - 10 into the debug log:
Integer count = 1; do { System.debug(count); count++; } while (count < 11);