SaasGuru Logo

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!
Control Flow Statements in Salesforce Apex

Control Flow Statements in Salesforce Apex

Control flow statements are the backbone of programming logic.  They allow you to direct the execution of your Apex code based on different conditions. In this blog post, we’ll focus on a key type of control flow statement and selection statements.

By reading this blog, you will learn:

  • The purpose of selection statements in Apex.
  • How to use the ‘if’ statement for basic decision-making.
  • Techniques for complex conditions with nested ‘if’ and ‘if-else-if’ structures.
  • Scenarios where selection statements improve your Salesforce development.

Let’s get started!

The control flow statements control the flow of the program’s executive orders. This order relies on the condition of logic and the data values.

saasguru salesforce labs

Types of Control Flow Statements

There are three types of control flow statements in the Salesforce Apex:

  • Selection Statements
  • Repetition Statements
  • Branching Statements.

Selection Statements

  • If
  • Nested If’s
  • the if-else-if ladder

Repetition Statements

  • while loops
  • do-while loops
  • for loops
  • for-each loop

Branching Statements

  • return statement

Also Read – Primitive Data Types in Salesforce

Selection Statements

If Statement:

  • The if case statement is an apex conditional branch explanation. It tends to be utilized in course program execution in two distinct ways.
  • Syntax of the if statement is explained as follows:

if (condition) statement1;

else statement;

  • Here, every statement might be a solitary assertion or a compound assertion encased in curly supports (that is, a block).
  • The condition is any articulation that profits a boolean worth. The else provision is optional.
  • It works like this: If the condition is satisfied, then statement is executed. Otherwise, statement will be executed(if it occurs).

Nested If Statement:

  • A nested if a case of another if or else. nested ifs are extremely normal in programming.
  • Syntax of nested if statement is as follows:

if boolean_expression_1{

/*Executes when the boolean expression 1 is true

if boolean_expression_2{

/*Executes when the boolean expression 2 is true

  • Here, the central concern to recollect is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not before associated or related with an else.

The if-else-if Ladder Statement:

  • The case statements are executed starting from the top. When one of the conditions controlling if the case meets true, the statement related to that in a case is executed, and the remainder is bypassed.
  • On the off chance that none of the conditions is valid, the last else statement will be executed. The else at the last goes about as a default condition; that is, assuming any remaining contingent tests fall flat, the last else statement is performed.
  • Assuming there is no last else and any remaining conditions are false, no action will take place. It looks like this:

if(condition) statement;

else if(condition) statement;

else statement;

Conclusion 

Now you have the power of selection statements at your fingertips! Experiment with ‘if’, ‘else if’, and nesting to build more dynamic and responsive Apex code.  Think of all the ways you can streamline processes and create custom logic within your Salesforce org.

Ready to take your control flow skills to the next level?  Unlock your full potential with saasguru. Start your free trial today and access 18+ Salesforce Certification Courses, 50+ Mock Exams, and 50+ Salesforce Labs for hands-on mastery.  

Transform your Salesforce development journey!

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Humans of Salesforce – Mauricio Alexandre Silva

Discover Mauricio Alexandre Silva’s inspiring Salesforce journey, tips for success, and insights into the evolving ecosystem. Read now!

Salesforce India Achieves $1 Billion Revenue Milestone

Salesforce India achieves $1 billion revenue milestone, driven by AI, innovation, and digital transformation across industries. Read now!

Most Demanded Courses in IIT Kanpur in 2025

Explore IIT Kanpur’s most demanded courses in 2025, including Salesforce, AI, Data Science, and more. Read now!