Salesforce Apex Tutorial – Chapter 9: Apex Decision Making

Salesforce Apex Tutorial – Chapter 9: Apex Decision Making

On October 22, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial – Chapter 9: Apex Decision Making

A decision-making structure contains one or more conditions that are to be evaluated by the program, along with a statement or statements that should be executed if the condition is true, and maybe other statements if it is false.

Here, we will discuss the structure of decision-making and conditional statements in the apex. In order to control how the execution flows when a condition is met, decision-making is necessary.

Following is the structure of decision-making used in most programming languages.

Apex Decision Making Flow-Chart-1

Description of above ER diagram/ structure as follows:

If statement: If statements contain a Boolean expression, one or more statements are followed by an If-statement.

For example:

if ([Boolean_condition]) {
// your logic
}

If-else statement: In If-else, the else part is always an optional part which executes when a given condition is false.

For example,

Integer x, sign;
// Your code
if (x <= 0) {
if (x == 0) {
sign = 0; 
} else  {
sign = -1;
}
}

If-elseIf-else: This statement is useful when we tested multiple conditions 

For example:

Integer x, sign= 2;
// Your code
if (x <= 0) {
sign = 0; 
} elseif  {
sign = -1;
}else {
sign = 2;
}

Nested if statement:  In an if or else if statement, we can put another if or else if statement(s) inside it. We use nested if statements for complex conditions.

For example:

if boolean_expression_1 {
/* Executes when the boolean expression 1 is true */
if boolean_expression_2 {
/* Executes when the boolean expression 2 is true */
}
Else{
/* Executes when the boolean expression is false
}
}
Comments are closed.