Comparison operators let your code ask questions about values. Every comparison produces a Boolean result: either true or false. Boolean values drive every decision your code makes, so these six operators are a big step forward.
Integer a = 10; Integer b = 5; System.debug(a == b); // false - equal to System.debug(a != b); // true - not equal to System.debug(a > b); // true - greater than System.debug(a < b); // false - less than System.debug(a >= b); // true - greater than or equal to System.debug(a <= b); // false - less than or equal to
Because a comparison is an expression that produces a value, you can store the result in a Boolean variable and use it later:
Integer score = 85; Boolean isHighScore = score > 80; System.debug(isHighScore); // true
A single equals sign = assigns a value to a variable. A double equals sign == compares two values. Mixing them up is one of the most common beginner mistakes, so read your code carefully:
Integer count = 5; // assignment: count now holds 5 Boolean isFive = count == 5; // comparison: true
Comparison operators are not limited to numbers. They also work on text and dates:
String name = 'Ada'; Boolean isAda = name == 'Ada'; // true Date today = Date.today(); Boolean isEarlier = today < today.addDays(7); // true
Whenever your code needs to make a decision, that decision starts with a comparison.