4.5 Operators and Control Statements
Salesforce Apex provides a variety of operators that you can use to perform different operations on variables or values. These operators include arithmetic operators, comparison operators, logical operators, assignment operators, and more. Let’s go through each type of operator:
Arithmetic Operators
Addition (+): Adds two values together.
Integer a = 5;
Integer b = 3;
Integer sum = a + b;
System.debug(‘Sum of a and b: ‘ + sum);
// Output: Sum of a and b: 8
Subtraction (-): Subtracts one value from another.
Integer a = 5;
Integer b = 3;
Integer difference = a – b; // Subtracting ‘b’ from ‘a’ System.debug(‘Difference between a and b: ‘ + difference);
Multiplication (*): Multiplies two values together.
Integer a = 5;
Integer b = 3;
Integer product = a * b; // Multiplying ‘a’ and ‘b’
System.debug(‘Product of a and b: ‘ + product);
Division (/): Divides one value by another.
Integer a = 10;
Integer b = 2;
Decimal quotient = (Decimal) a / b; // Dividing ‘a’ by ‘b’ System.debug(‘Quotient of a and b: ‘ + quotient);
Increment (++) and Decrement (–): Increases or decreases the value of a variable by 1.
Integer count = 5;
count++; // Equivalent to: count = count + 1;
System.debug(‘Updated count: ‘ + count);
// Output: Updated count: 6
Another example by decreasing the value.
Integer count = 8;
count–; // Equivalent to: count = count – 1;
System.debug(‘Updated count: ‘ + count);
// Output: Updated count: 7
Comparison Operators
Equality (==) and Inequality (!=): Checks if two values are equal or not as in two examples with integer and string type as follows:
Integer num1 = 5;
Integer num2 = 5;
Boolean areEqual = num1 == num2;
if (areEqual) {
System.debug(‘num1 and num2 are equal.’); } else {
System.debug(‘num1 and num2 are not equal.’);
}
Another example with string value.
String str1 = ‘Hello’;
String str2 = ‘World’;
Boolean areStringsNotEqual = str1 != str2;
if (areStringsNotEqual) {
System.debug(‘str1 and str2 are not equal.’); } else {
System.debug(‘str1 and str2 are equal.’);
}
Greater than (>): Checks if one value is greater than another.
Integer num1 = 10;
Integer num2 = 5;
if (num1 > num2) {
System.debug(‘num1 is greater than num2.’); } else {
System.debug(‘num1 is not greater than num2.’);
}
Less than (<): Checks if one value is less than another.
Integer num1 = 5;
Integer num2 = 10;
if (num1 < num2) {
System.debug(‘num1 is less than num2.’); } else {
System.debug(‘num1 is not less than num2.’);
}
Greater than or equal to (>=): Checks if one value is greater than or equal to another.
Integer num1 = 10;
Integer num2 = 10;
if (num1 >= num2) {
System.debug(‘num1 is greater than or equal to num2.’); } else {
System.debug(‘num1 is less than num2.’);
}
Less than or equal to (<=): Checks if one value is less than or equal to another.
Integer num1 = 5;
Integer num2 = 5;
if (num1 <= num2) {
System.debug(‘num1 is less than or equal to num2.’); } else {
System.debug(‘num1 is greater than num2.’);
}
Logical Operators
AND (&&): Returns true if both conditions are true.
Boolean isRaining = true;
Boolean hasUmbrella = true;
if (isRaining && hasUmbrella) {
System.debug(‘You can go out with an umbrella.’);
} else { System.debug
(‘It\’s either not raining or you don\’t have an umbrella.’);
}
OR (||): Returns true if at least one of the conditions is true.
Boolean hasPermission = true;
Boolean isAdmin = false;
if (hasPermission || isAdmin) {
System.debug(‘You have permission to access.’);
} else {
System.debug(‘You do not have permission to access.’);
}
NOT (!): Returns the opposite boolean value of the condition.
Boolean hasPermission = false;
if (!hasPermission) {
System.debug(‘You do not have permission to access.’); } else {
System.debug(‘You have permission to access.’);
}
Assignment Operators
Assign (=): Assigns a value to a variable.
Integer num1 = 5;
Integer num2;
num2 = num1;
System.debug(‘num1: ‘ + num1);
System.debug(‘num2: ‘ + num2);
Addition assignment (+=): Adds a value to the current value of a variable and assigns the result to the same variable.
Integer total = 10;
Integer increment = 5;
total += increment;
System.debug(‘Updated total: ‘ + total);
// Output: Updated total: 15
Subtraction assignment (-=): Subtracts a value from the current value of a variable and assigns the result to the same variable.
Integer total = 20;
Integer decrement = 8;
total -= decrement;
System.debug(‘Updated total: ‘ + total);
// Output: Updated total: 12
Multiplication assignment (*=): Multiplies the current value of a variable by a value and assigns the result to the same variable.
Integer base = 5;
Integer multiplier = 3;
base *= multiplier; // Equivalent to: base = base * multiplier;
System.debug(‘Result: ‘ + base); // Output: Result: 15