This blog post describes the use of if-else statements, switch statements, and loops in Salesforce to manage the flow of code execution.
Like other Programming Languages, Apex also Provides control flow statements like If-else, Switch and Loops to control the flow of the execution of the program. Statements are usually implemented in line by line manner as they are present. Through Control flow statements, we can implement the apex code according to the particular condition. To ease the execution of complex or difficult programs, Developer will use control flow statements.
Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Training |
The conditional Statements of Apex Programming are as follows:
The syntax of “If-Else” Statement is as follows:
If([condition])
Statement
else
statement
Example
Integer j, value;
if(j%2==0) value=0;
else value=-1;
In the Nested If statement, one “If” is placed in another “If”. one “If” focuses on another “If” statement. Example:
if( a==5) {
if(b<10) c=d;
if(e>50) f=g;
else c=f;
}
else c=g;
“ If-Else-if Ladder” is a general program construct that is completely based on the order of nested If. Syntax of the if-else-if ladder is as follows:
if(condition)
statement1;
else if(condition1)
statement2;
elseif(condition2)
statement3;
else
statement4;
“If” statements are implemented in “Top-down” order. If the conditions monitoring the “If” statement are true, then that “If” statement is executed.
Apex facilitates a switch statement that checks whether the given expression suits one of the given conditions.
Syntax of the Switch statement is as follows:
Switch {
When quality1{
Statement1
}
When quality2{
Statement2
}
When quality3{
Statement3
}
When else{
Statement4
}
In Apex, we will have the following Loop Statements;
In apex, while loop implements a set of statements repeatedly until the condition remains true. The syntax of While loop is as follows:
while(condition1) {
Statement1
}
Example
Integer j=1;
while(j<5)
{
System.debug(j);
J++;
}
The above, while Program prints the numerical values from 1 to 5.
The condition present in the while loop controls the implementation of the statements present in the while block. To implement the statements at least one time, while loop checks the condition, but in do-while, the statements are implemented at least once irrespective of the given condition.
The syntax of Do-while loop is as follows:
do {
Statements;
}while(condition1)
Example:
Integer e=5;
do
{
system.debug(e);
e++;
}while(e<15);
In the above apex program, first, the value of e is printed through the “system. Debug” statement, and then the condition present in the while loop is tested. Based on that condition, the values of e are printed. So by looking at the given condition, we can say that values from 5 to 14 are printed.
Output:
5 6 7 8 9 10 11 12 13 14
Apex Programming supports three types of “for” loops; they are as follows:
Syntax
for(initialisation ; condition; increment/Decrement)
{
Statement;
}
The conventional for loop contains three statements, which are as follows:
Example:
for(Integer e=1; e<15;e++)
{
system.debug(“ e+3”);
}
Syntax:
for(variable; set_or_list)
{
Statements1
}
While implementing this loop, the Apex engine allocates the variable to every item of set_or_list and implements the statements for every value.
Example:
Int[] newInt = new Int[] {10,75, 25, 55, 95, 50, 23, 63, 78, 89};
for(Int e : newint) {
system.debug(e);
}
The elements or items present in the “Int” array are stored in the variable “e”.The items are printed sequentially through “system—Debug” statement.
Collections contain maps, sets or lists. Changing the elements of the collection, while iterating it generates errors. We cannot insert or remove elements directly from them.
Adding or Inserting Elements at the time of Iteration
The elements to be added to the collection should be stored in a temporary list, and we can add them after completing the iteration of that collection.
Removing Elements at the time of Iteration
The elements to be removed should be kept in a list, and they can be removed after completing the iteration of the collection.
Apex programming supports different types of conditional statements or loops to control the flow of program execution. If the developer or programmer wants to start the program execution from a particular statement then they will use conditional statements. If the developers want to execute a particular statement repeatedly, then they will Loop or iterative statements. So, to execute the statements based on our requirements we will use control flow statements.
In the next topic, we will discuss in detail “Salesforce Collection - List”. Keep following us for more info on Salesforce Development / Programming.
Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.
Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.
These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Name | Dates | |
---|---|---|
Salesforce Training | Nov 02 to Nov 17 | View Details |
Salesforce Training | Nov 05 to Nov 20 | View Details |
Salesforce Training | Nov 09 to Nov 24 | View Details |
Salesforce Training | Nov 12 to Nov 27 | View Details |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.