Apex Control Statements (if, switch)

Apex Control Statements

Control statements are used to make decisions in your Apex programs. Based on certain conditions, they decide which block of code should be executed. 

 

In Apex, the two main types of decision-making control statements are: 

if, if-else, if-else if – for conditional logic
switch – for multiple condition checking (introduced in Apex from version 50.0)

1. if Statement

Executes a block of code only if the condition is true. 

Syntex: 

if (condition) {
// Code to run if condition is true
}

2. if-else Statement

Executes one block if the condition is true, and another block if it is false. 

Syntex: 

if (condition) {
// Code if true
} else {
// Code if false
}

3. if-else Statement

Used when you have multiple conditions to check. 

Syntex: 

if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none are true
}

4. switch Statement (Simpler alternative to many if-else)

The switch statement works only with supported types: Integer, Long, String, Enum, and SObject. 

Syntex: 

switch on variable {
when value1 {
// Code if value1 matches
}
when value2 {
// Code if value2 matches
}
when else {
// Code if no match found
}
}

Code Example:

1. Use of if-else Statement

Integer marks = 75;

if (marks >= 80) {
System.debug(‘Grade: A’);
} else {
System.debug(‘Grade: B’);
}

Code Explanation:

Integer marks = 75;: Initializes a variable marks with the value 75.
if (marks >= 80): Checks if marks is **greater than or equal to 80. In this case, 75 >= 80isfalse, so the block inside if` is skipped.
else: Since the if condition is false, the code inside the else block runs.
System.debug(‘Grade: B’);: Prints ‘Grade: B’.

Output:

Grade:B

2. if-else for Age Check

Integer age = 17;

if (age >= 18) {
System.debug(‘Adult’);
} else if (age >= 13) {
System.debug(‘Teenager’);
} else {
System.debug(‘Child’);
}

Code Explanation:

Integer age = 17;: Initializes the variable age with the value 17.
if (age >= 18): Checks if age is 18 or older. → 17 >= 18 is false, so this block is skipped.
else if (age >= 13): Checks if age is 13 or older (but less than 18). → 17 >= 13 is true, so this block executes.
System.debug(‘Teenager’);: Prints ‘Teenager’ because the condition above is true.
else: This part is skipped because the else if condition was already met.

Output:

Teenagar

3. switch Statement for Day of Week

String day = ‘Monday’;

switch on day {
when ‘Saturday’, ‘Sunday’ {
System.debug(‘Weekend’);
}
when ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’ {
System.debug(‘Weekday’);
}
when else {
System.debug(‘Invalid day’);
}
}

Code Explanation:

String day = ‘Monday’;: Initializes the variable day with the string ‘Monday’.
switch on day { … }: Starts a switch block to compare the value of day.
when ‘Saturday’, ‘Sunday’: If day matches ‘Saturday’ or ‘Sunday’, it prints ‘Weekend’. → This condition is false (day is ‘Monday’), so skipped.
when ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’: If day matches any of these, it prints ‘Weekday’. → ‘Monday’ matches, so this block runs.
when else: This is the default block for unmatched cases. Skipped in this case.

Output:

Weekday

4. Switch on Integer

Integer option = 2;

switch on option {
when 1 {
System.debug(‘Option 1 selected’);
}
when 2 {
System.debug(‘Option 2 selected’);
}
when else {
System.debug(‘Invalid option’);
}
}

Code Explanation:

• Integer option = 2;: Initializes the variable option with the value 2.
• switch on option { … }: Starts a switch block to check the value of option.
• when 1: Runs if option == 1. → False, so skipped. • when 2: Runs if option == 2. → True, so this block executes.
• System.debug(‘Option 2 selected’);: Prints a message indicating that Option 2 was selected.
• when else: Acts as the default case for unmatched values. Skipped here.

Output:

Option 2 selected

Tasks for Practice

Task 1: Write an Apex program that checks whether a given number is even or odd using the if-else statement.

Task 2: Write an Apex program that checks a person's age and prints:

• “Child” if age < 13
• “Teenager” if age is between 13 and 17
• “Adult” if age is 18 or more

Task 3: Create a switch statement that takes a string variable fruit and prints:

• “Red” if it’s “Apple”
• “Yellow” if it’s “Banana”
• “Orange” if it’s “Mango”
• “Unknown color” for anything else

Task 4: Create an Apex program to compare two numbers and print:

“Equal” if they are equal
“First is greater” if the first number is larger
“Second is greater” otherwise

Task 5: Using if-else, check if a number is positive, negative, or zero.

Task 6: Create a switch statement for an integer month that prints:

• “Winter” for months 12, 1, 2
• “Spring” for 3, 4, 5
• “Summer” for 6, 7, 8
• “Autumn” for 9, 10, 11

Task 7: Create a program that takes a student's score and prints their grade using nested if-else logic:

• 90 and above → A
• 80 to 89 → B
• 70 to 79 → C
• Below 70 → F