Apex Conditional Logic

Of course, like all programming languages, Apex includes syntax to implement conditional logic, otherwise known as IF – THEN – ELSE logic.

The basic format for this type of code is the following:

if (Boolean_expression)
 Statement;
 else
 Statement;

Note that the Boolean must be within parentheses, even if it is a simple expression, and you need to have semi-colons to indicate the end of each statement in the conditional logic.

The form shown above includes a single statement for each condition. Normally, you create multiple statements for a condition, requiring the use of curly braces to group the statements, as in the following:

if (Boolean_expression){
 statement;
 statement;
 statement;
 statement;}
else {
 statement;
 statement ...

Get The Developer’s Guide to the Force.com Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.