Division assignment (/=): Divides the current value of a variable by a value and assigns the result to the same variable.
Decimal dividend = 20.0;
Decimal divisor = 4.0;
dividend /= divisor; // Equivalent to: dividend = dividend /divisor;
System.debug(‘Result: ‘ + dividend); // Output: Result: 5.0
Control statements in Apex are used to control the flow of execution in a program. They enable you to make decisions, repeat actions, and execute different parts of code based on conditions. Here are some common control statements in Apex along with examples:
if Statement: The if statement is used to execute a block of code if a specified condition is true. It is a fundamental control structure in programming. It allows you to execute certain blocks of code based on whether a given condition is true or false. Let’s dive into the details of how the if statement works. The basic syntax of an if statement is as follows:
Integer x = 10;
if (x > 5) {
System.debug(‘x is greater than 5’); } else {
System.debug(‘x is not greater than 5’);
}
The if statement checks whether x is greater than 5. Since x is indeed 10, which is greater than 5, the condition evaluates to true.
As a result, the code inside the curly braces following the if statement is executed, and the debug message “x is greater than 5” is printed.
The else block is not executed since the if condition is satisfied.
If the value of x were less than or equal to 5, then the condition would be false, and the code within the else block would be executed.
Remember that the else block is optional. If you only want to execute code when the condition is true and do nothing when it’s false, you can omit the else block altogether.
The if statement is the building block for more complex decision-making processes in programming. You can also nest multiple if statements inside each other, or use else if statements to handle multiple conditions. This allows you to create more intricate decision trees based on various conditions.
switch Statement: The switch statement is used to evaluate a variable or expression against multiple possible values and execute different code blocks based on the matched value. It provides a way to streamline multiple if and else if conditions when you have
a specific value to check against. The switch statement is particularly useful when you have a set of discrete values you want to compare against a single variable. Here’s an example using the switch statement in Apex:
String fruit = ‘apple’;
switch on fruit {
when ‘apple’ {
System.debug(‘You selected an apple.’);
}
when ‘banana’ {
System.debug(‘You selected a banana.’);
}
when else {
System.debug(‘You selected a different fruit.’);
}
}
In this example, the switch statement compares the value of the fruit variable against different cases. Since fruit is “apple,” the first case is matched, and the corresponding message is printed. Keep in mind that the switch statement in Apex only works with equality comparisons. It cannot handle more complex conditions like comparisons using greater than or less than operators. If you need to evaluate conditions beyond simple equality, you would need to use an if statement or a combination of if and else if statements.