Curriculum
Control Statements are programming structures that determine the flow of execution in an Apex program. They allow developers to make decisions, repeat actions, execute code conditionally, and control how business logic behaves under different circumstances.
In Salesforce development, Control Statements are used in Apex Classes, Triggers, Batch Jobs, Integrations, and Automation Solutions. They help developers implement validation rules, approval logic, lead assignment processes, enrollment workflows, payment processing systems, and many other business operations.
Understanding Control Statements is essential because they form the foundation of decision-making and repetitive processing in Apex programming.
Control Statements are programming constructs that control the order in which statements execute.
They help developers:
Without Control Statements, programs would execute sequentially without any flexibility.
Organizations frequently need logic such as:
Control Statements make these operations possible.
Execute logic based on conditions.
Reduce manual effort.
Handle multiple scenarios.
Avoid repetitive coding.
Support complex applications.
These benefits make Control Statements essential for software development.
Apex supports three major categories.
Each category serves a specific purpose.
Selection Statements help programs make decisions.
Examples:
These statements execute code based on conditions.
The if statement executes code only when a condition is true.
Syntax:
if(condition){
// code
}
Integer age = 20;
if(age >= 18){
System.debug('Eligible');
}
Output:
Eligible
The code executes because the condition is true.
Step 1:
Evaluate condition.
Step 2:
If condition is true:
Execute block.
Step 3:
If condition is false:
Skip block.
This is the simplest decision-making structure.
Boolean feePaid = true;
if(feePaid){
System.debug('Enrollment Confirmed');
}
Output:
Enrollment Confirmed
Used frequently in business applications.
if-else provides two possible execution paths.
Syntax:
if(condition){
// true block
}
else{
// false block
}
Integer marks = 35;
if(marks >= 40){
System.debug('Pass');
}
else{
System.debug('Fail');
}
Output:
Fail
One of the two blocks always executes.
Boolean paymentStatus = false;
if(paymentStatus){
System.debug('Course Access Granted');
}
else{
System.debug('Payment Pending');
}
Output:
Payment Pending
Useful in payment systems.
Used when multiple conditions must be checked.
Syntax:
if(condition1){
}
else if(condition2){
}
else{
}
Integer marks = 85;
if(marks >= 90){
System.debug('Grade A+');
}
else if(marks >= 75){
System.debug('Grade A');
}
else{
System.debug('Grade B');
}
Output:
Grade A
Useful when several possibilities exist.
Decimal discount = 15;
if(discount >= 30){
System.debug('Gold Customer');
}
else if(discount >= 15){
System.debug('Silver Customer');
}
else{
System.debug('Regular Customer');
}
Output:
Silver Customer
Common in customer management systems.
An if statement inside another if statement.
Example:
Integer age = 25;
Boolean feePaid = true;
if(age >= 18){
if(feePaid){
System.debug('Admission Approved');
}
}
Output:
Admission Approved
Nested conditions support complex business logic.
The switch statement evaluates multiple values efficiently.
Syntax:
switch on value {
when value1 {
}
when value2 {
}
when else {
}
}
Switch improves readability when many conditions exist.
String day = 'Monday';
switch on day {
when 'Monday' {
System.debug('Start of Week');
}
when 'Friday' {
System.debug('Weekend Near');
}
when else {
System.debug('Regular Day');
}
}
Output:
Start of Week
Switch statements simplify multiple comparisons.
Iteration Statements repeat code multiple times.
Examples:
Loops reduce repetitive coding.
The for loop executes code repeatedly for a specified number of times.
Syntax:
for(initialization; condition; update){
}
for(Integer i = 1; i <= 5; i++){
System.debug(i);
}
Output:
1
2
3
4
5
The loop repeats five times.
for(Integer student = 1;
student <= 3;
student++){
System.debug(
'Processing Student'
);
}
Output:
Processing Student
Processing Student
Processing Student
Used in bulk processing.
A while loop executes as long as a condition remains true.
Syntax:
while(condition){
}
Integer count = 1;
while(count <= 3){
System.debug(count);
count++;
}
Output:
1
2
3
The condition is checked before execution.
Integer pendingPayments = 3;
while(pendingPayments > 0){
System.debug(
'Sending Reminder'
);
pendingPayments--;
}
Output:
Sending Reminder
Sending Reminder
Sending Reminder
Useful for repetitive processing.
The do-while loop executes at least once.
Syntax:
do{
}
while(condition);
Integer count = 1;
do{
System.debug(count);
count++;
}
while(count <= 3);
Output:
1
2
3
Execution occurs before condition checking.
| Feature | while | do-while |
|---|---|---|
| Condition Checked | Before Execution | After Execution |
| Minimum Execution | 0 Times | 1 Time |
| Performance | Similar | Similar |
Understanding the difference helps choose the right loop.
Branching Statements alter loop execution.
Examples:
These improve control over loops.
break immediately terminates a loop.
Example:
for(Integer i = 1; i <= 10; i++){
if(i == 5){
break;
}
System.debug(i);
}
Output:
1
2
3
4
The loop stops when i equals 5.
continue skips the current iteration.
Example:
for(Integer i = 1; i <= 5; i++){
if(i == 3){
continue;
}
System.debug(i);
}
Output:
1
2
4
5
The value 3 is skipped.
Always define exit conditions.
Choose the correct loop structure.
Improve performance.
Prevent Governor Limit issues.
Ensure accuracy.
These practices improve Apex performance.
Developers should avoid these mistakes.
A software training company processes student enrollments.
List<String> students =
new List<String>{
'Rahul',
'Priya',
'Amit'
};
for(String student : students){
System.debug(
'Processing: ' + student
);
}
Output:
Processing: Rahul
Processing: Priya
Processing: Amit
This demonstrates practical loop usage.
Understanding Control Statements helps professionals:
Control Statements are used in virtually every Apex program.
Control Statements determine how an Apex program executes. Through Selection Statements such as if, if-else, else-if, and switch, developers can implement decision-making logic. Through Iteration Statements such as for, while, and do-while loops, developers can process data efficiently. Branching Statements such as break and continue provide additional control over execution flow. Mastering Control Statements is essential for building robust Salesforce applications and automation solutions.
Control Statements manage the flow of execution in an Apex program.
An if statement executes code only when a condition is true.
if-else handles two outcomes, while else-if supports multiple conditions.
A for Loop repeats code a specified number of times.
A while loop checks the condition before execution, while a do-while loop executes at least once before checking the condition.
They allow developers to make decisions, automate processes, and control program execution.
Looking to learn more technologies and programming skills?
Send us your inquiry and start your journey towards a successful IT career.
✔ 100% Secure ✔ No Spam ✔ Instant Response
WhatsApp us