• Lessons Home
Topics
Community
  1. Lessons
  2. Arithmetic & Logic
  3. Logical Expressions
  4. Comparison Operators

      Comparison Operators

      Comparison Operators

      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.

      The Six Comparison Operators

      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
      

      Comparisons Produce Boolean Values

      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 Common Trap: = versus ==

      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
      

      More Than Numbers

      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.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In