Chapter 5: Operators and Expressions
Objectives
By the end of this chapter you will be able to:
- Use arithmetic, comparison, and logical operators correctly in AL
- Write readable expressions for business rules
- Avoid precedence mistakes that silently change behavior
- Prepare expression patterns used in table triggers and codeunits
5.1 Arithmetic operators in business logic
AL supports +, -, *, and / for numeric calculations.
procedure CalculateLineTotal(Quantity: Decimal; UnitPrice: Decimal; DiscountPct: Decimal): Decimal
begin
exit((Quantity * UnitPrice) * (1 - DiscountPct));
end;
Use Decimal for money calculations to avoid rounding surprises.
5.2 Comparison and logical operators
Validation and branching are built on comparison operators (=, <>, <, >, <=, >=) and logical operators (and, or, not).
if (Amount > 0) and (DueDate <> 0D) then
Message('Installment is valid for posting.');
When you combine conditions, use explicit parentheses.
5.3 Expression clarity and maintainability
A long expression only counts as correct if future readers can audit it quickly. To keep yours easy to check:
- Break complex formulas into named local variables.
- Don’t duplicate the same calculation across procedures.
- Make the business intent obvious, for example with an
IsOverduevariable.
These choices make the performance work in Chapter 13 and the testing in Chapter 14 much easier.
5.4 Expression evaluation flow (visual guide)
Use this flow as a reminder of the evaluation order and where the validation checks happen:
flowchart TD
A[Read inputs] --> B{Quantity > 0?}
B -->|No| C[Error: invalid quantity]
B -->|Yes| D{UnitPrice >= 0?}
D -->|No| E[Error: invalid unit price]
D -->|Yes| F[Gross = Quantity * UnitPrice]
F --> G[”Discounted = Gross * (1 - DiscountPct)”]
G --> H[Return line total]
Chapter summary
In this chapter you:
- Used arithmetic, comparison, and logical operators in realistic AL scenarios
- Practiced writing expressions that are both correct and readable
- Learned patterns that reduce hidden logic bugs in later chapters
Tasks
- Operator warmup. Write one AL example each for arithmetic, comparison, and logical operators using loan-installment values.
- Formula extraction. Replace one long inline expression with named local variables (
GrossAmount,DiscountAmount,NetAmount). - Precedence check. Evaluate the same formula once without parentheses and once with parentheses; explain the difference.
- Condition hardening. Rewrite one multi-condition
ifwith explicit parentheses and positive naming (IsDue,HasAmount, etc.). - Validation expression. Add one expression that rejects invalid installment input (
Amount <= 0, missing date, or invalid status combination). - Try it yourself. Create a boolean expression for “ready to post” that uses at least three conditions and document why each is needed.
Check your work: compare your answers with the Chapter 5 solutions, then ensure your expressions remain reusable in Chapter 7 triggers and Chapter 9 codeunit procedures.