Logical Operators in Practice
By the end of this lesson, you'll be able to:
- Combine multiple conditions inside an if statement using && and ||
- Use ! to invert a condition inside an if statement
- Read a compound if condition and explain what it requires
Prerequisites: "If, Else If, and Else"
Combining conditions inside an if
Boolean isActive = true;
Integer daysOverdue = 15;
if (isActive && daysOverdue > 10) {
System.debug('Send overdue payment reminder');
}
Both conditions must be true for the reminder to trigger — exactly the same && from Module 4, just now directly shaping an if statement's behavior.
A real business example: Loan Processing
Decimal creditScore = 720;
Decimal existingDebt = 15000;
if (creditScore >= 650 || existingDebt < 5000) {
System.debug('Pre-approved for review');
}
This encodes "either a decent credit score OR low existing debt is enough to pre-qualify" — a realistic underwriting shortcut expressed in one line thanks to ||.
Common mistakes
- Confusing && and || under pressure.
&&requires everything to be true;||requires just one. When a compound condition isn't behaving as expected, this mix-up is the first thing worth checking.
Exercise
Declare Boolean isVip = false and Decimal cartTotal = 120. Debug 'Free shipping' if the customer is VIP OR the cart total is at least 100.
Show hint
isVip || cartTotal >= 100
Logical Operators in Practice Quiz
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's &&, ||, and ! operators become genuinely useful once they're driving real if statements — most real business rules need more than one condition at once.