Home  >  Blog  >   Salesforce  > 

Controlling the program flow in Salesforce

This blog post describes the use of if-else statements, switch statements, and loops in Salesforce to manage the flow of code execution.

Rating: 5
  
 
3425
  1. Share:
Salesforce Articles

About Controlling Statements

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 

Conditional Statements

The conditional Statements of Apex Programming are as follows:

1. “If-Else” Statement

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;

2. Nested If

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;

3. else-if Ladder

“ 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.

 MindMajix YouTube Channel

4. Switch Statement

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
}

Loops or Iterative Statements

In Apex, we will have the following Loop Statements;

1. While Loop

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.

2. Do-while Loop

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  

For Loops

Apex Programming supports three types of “for” loops; they are as follows:

1. Traditional for loop

    Syntax

for(initialisation ; condition; increment/Decrement)

{

   Statement;

}

The conventional for loop contains three statements, which are as follows:

  • Initialisation: It initialises the variable
  • Condition: A condition that controls the execution of statements.
  • Increment/Decrement: It increases or decreases the values of the variables.

Example:

for(Integer e=1; e<15;e++)

{

system.debug(“ e+3”);

}


2. Set or List Iteration For Loop

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.

3. Iterating Collections

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.

Conclusion

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

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training  Salesforce Lightning Training 
Salesforce Advanced Developer Training  Salesforce Developer Training 
Salesforce IoT Training  Salesforce App Builder Certification Training 
Salesforce AppExchange Training  Salesforce Service Cloud Training 
and many more..  
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingAug 05 to Aug 20
Salesforce TrainingAug 08 to Aug 23
Salesforce TrainingAug 12 to Aug 27
Salesforce TrainingAug 15 to Aug 30
Last updated: 04 August 2023
About Author
Remy Sharp
Arogyalokesh

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.

Recommended Courses

1 /15