If Statements
By the end of this lesson, you'll be able to:
- Write a basic if statement
- Explain what happens when the condition is false and there is no else
- Use curly braces correctly, even for a single statement
Prerequisites: "Comparison Operators in Practice"
The basic shape
Integer stockLevel = 3;
if (stockLevel < 5) {
System.debug('Low stock warning!');
}
Integer stockLevel = 3;— the current stock count.- (blank line)
if (stockLevel < 5) {— the condition inside( )must evaluate to aBoolean. Here it'strue, so the block below runs.System.debug('Low stock warning!');— runs because line 3 was true.}— closes theifblock. If line 3 had beenfalse, this whole block would simply be skipped and execution would continue after line 5.
A real business example: Inventory (Manufacturing)
Integer rawMaterialUnits = 120;
Integer reorderThreshold = 100;
if (rawMaterialUnits < reorderThreshold) {
System.debug('Trigger a purchase order for raw materials.');
}
Integer rawMaterialUnits = 120;— units currently on hand.Integer reorderThreshold = 100;— the reorder point.- (blank line)
if (rawMaterialUnits < reorderThreshold) {—falsehere (120 is not less than 100), so the block below is skipped.System.debug('Trigger a purchase order for raw materials.');— does not run in this case.}— closes theifblock.
This is a real, common manufacturing rule expressed directly as an if statement — no framework, no magic, just a condition and a reaction.
Common mistakes
- Skipping the curly braces for a single-line if. Apex allows
if (x) doSomething();without braces, but it's fragile — the next line someone adds might silently fall outside theifwithout them noticing. Always use{ }, even for one line. - Putting an assignment (=) instead of a comparison (==) in the condition.
if (x = 5)is a compile error in Apex for primitive Booleans — a helpful safety net compared to some other languages.
What happens with no else
Integer temperature = 22;
if (temperature > 30) {
System.debug('Too hot!');
}
System.debug('Done checking temperature.');
Integer temperature = 22;— the reading.- (blank line)
if (temperature > 30) {—false, since 22 is not greater than 30.System.debug('Too hot!');— skipped — this never runs.}— closes theifblock.- (blank line)
System.debug('Done checking temperature.');— runs regardless of what happened above; it isn't inside theifblock, so it always executes.
Exercise
Declare Integer ticketsOpen = 8. Write an if statement that debugs 'Escalate to manager' when ticketsOpen is greater than 5.
Show hint
if (ticketsOpen > 5) { ... }
Exercise
Coding challenge: write an if statement that debugs "Free shipping applied" only when an order total is 500 or more.
Show hint
Use >= for "500 or more."
If Statements Quiz
Leaderboard
- 1. Monde Masiza 2/2
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
An if statement runs a block of code only when its condition evaluates to true — the single most common way Apex code branches based on data.