Beginner 15 min read

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!');
}
  1. Integer stockLevel = 3; — the current stock count.
  2. (blank line)
  3. if (stockLevel < 5) { — the condition inside ( ) must evaluate to a Boolean. Here it's true, so the block below runs.
  4. System.debug('Low stock warning!'); — runs because line 3 was true.
  5. } — closes the if block. If line 3 had been false, 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.');
}
  1. Integer rawMaterialUnits = 120; — units currently on hand.
  2. Integer reorderThreshold = 100; — the reorder point.
  3. (blank line)
  4. if (rawMaterialUnits < reorderThreshold) { — false here (120 is not less than 100), so the block below is skipped.
  5. System.debug('Trigger a purchase order for raw materials.'); — does not run in this case.
  6. } — closes the if block.

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 the if without 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.');
  1. Integer temperature = 22; — the reading.
  2. (blank line)
  3. if (temperature > 30) { — false, since 22 is not greater than 30.
  4. System.debug('Too hot!'); — skipped — this never runs.
  5. } — closes the if block.
  6. (blank line)
  7. System.debug('Done checking temperature.'); — runs regardless of what happened above; it isn't inside the if block, 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) { ... }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

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."

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

If Statements Quiz

1. What happens if an if statement's condition is false and there is no else?

2. It is good practice to always use curly braces with if, even for a single statement.

Log in to submit the quiz and save your score.

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.