Beginner 15 min read

Comparison Operators in Practice

By the end of this lesson, you'll be able to:

  • Recall the six comparison operators from Module 4
  • Recognize that a comparison expression is the foundation of every decision Apex makes
  • Write a comparison expression suited to a real business rule

Prerequisites: Module 5: "Enums"

A quick recap, with intent

You already know ==, !=, >, <, >=, and <= from Module 4. What's new here isn't the operators — it's the purpose. Every one of these produces a Boolean, and that Boolean is what an if statement (next lesson) actually reacts to.

Integer accountAge = 3;
Boolean isEstablished = accountAge >= 2;
  1. Integer accountAge = 3; — the value being evaluated.
  2. Boolean isEstablished = accountAge >= 2; — the comparison produces a Boolean, true, that's ready to drive a decision.

isEstablished isn't just a value — it's a decision waiting to be acted on.

A real business example: Sales Automation

Decimal dealSize = 75000;
Boolean requiresExecutiveApproval = dealSize > 50000;
  1. Decimal dealSize = 75000; — the deal amount.
  2. Boolean requiresExecutiveApproval = dealSize > 50000; — true, since 75000 is over the 50000 threshold.

This single comparison encodes a real company policy: deals over R50,000 need executive sign-off. The next lesson turns this Boolean into actual branching behavior.

Exercise

Declare Integer daysSinceLastOrder = 45. Declare a Boolean called isAtRiskCustomer that's true if it's been more than 30 days. Debug it.

Show hint

daysSinceLastOrder > 30

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Comparison Operators in Practice Quiz

1. What does every comparison expression ultimately drive in Apex?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Module 4 introduced the comparison operators as pure syntax; this lesson bridges into using them for their real purpose — driving decisions — which is exactly what the rest of this module builds on.