Beginner 30 min read

Build the Operations

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

  • Implement each of the four arithmetic operations as its own path in the code
  • Use a switch statement to route to the correct operation based on the operator
  • Combine variables and expressions (Modules 3-4) with a switch statement (Module 6)

Prerequisites: "Plan the Program"

Routing to the right operation

Decimal a = 12;
Decimal b = 5;
String operatorSymbol = '+';
Decimal result;

switch on operatorSymbol {
    when '+' {
        result = a + b;
    }
    when '-' {
        result = a - b;
    }
    when '*' {
        result = a * b;
    }
    when '/' {
        result = a / b;
    }
}

System.debug(result); // 17
  1. Decimal a = 12; — the first operand.
  2. Decimal b = 5; — the second operand.
  3. String operatorSymbol = '+'; — which operation to perform.
  4. Decimal result; — declared, not yet assigned — will hold whichever calculation runs below.
  5. (blank line)
  6. switch on operatorSymbol { — this is exactly the switch on pattern from Module 6: operatorSymbol is checked once, and the matching branch below runs. 7-9. when '+' { result = a + b; } — matches here, since operatorSymbol is '+'. 10-12. when '-' { result = a - b; } — not checked once line 7 already matched. 13-15. when '*' { result = a * b; } — not checked. 16-18. when '/' { result = a / b; } — not checked.
  7. } — closes the switch block.
  8. (blank line)
  9. System.debug(result); — logs 17, from 12 + 5.

Trying more than one calculation

Decimal a = 12;
Decimal b = 5;
Decimal sum;
Decimal product;

switch on 'add' {
    when 'add' {
        sum = a + b;
    }
}
System.debug(sum); // 17

switch on 'multiply' {
    when 'multiply' {
        product = a * b;
    }
}
System.debug(product); // 60
  1. Decimal a = 12; — first operand.
  2. Decimal b = 5; — second operand.
  3. Decimal sum; — will hold the addition result.
  4. Decimal product; — will hold the multiplication result.
  5. (blank line)
  6. switch on 'add' { — checking the literal text 'add' directly this time, instead of a variable.
  7. when 'add' { — matches.
  8. sum = a + b; — runs.
  9. } — closes the when.
  10. } — closes the switch.
  11. System.debug(sum); — logs 17.
  12. (blank line)
  13. switch on 'multiply' { — a second, separate switch.
  14. when 'multiply' { — matches.
  15. product = a * b; — runs.
  16. } — closes the when.
  17. } — closes the switch.
  18. System.debug(product); — logs 60.

Each calculation is just the same variables and switch pattern, run again with a different operator. This is the working core of the calculator — the next two lessons make it safe and presentable.

A note on what comes next

Right now, every calculation means retyping the same switch block with different variable values — repetitive, and a little error-prone. That repetition is exactly the problem a method solves: a named, reusable block of instructions you can call again and again with different inputs, instead of retyping it. Module 9 teaches methods properly, and by the end of that module you'll be able to wrap this exact logic into one clean, reusable calculate(a, b, operator) method. For now, focus on getting the calculation logic itself correct.

The calculation logic so far

Decimal a = 12;
Decimal b = 5;
String operatorSymbol = '+';
Decimal result;

switch on operatorSymbol {
    when '+' {
        result = a + b;
    }
    when '-' {
        result = a - b;
    }
    when '*' {
        result = a * b;
    }
    when '/' {
        result = a / b;
    }
}

System.debug(result);

1-4. Decimal a = 12; Decimal b = 5; String operatorSymbol = '+'; Decimal result; — the two operands, the chosen operator, and a place to hold the answer. 5. (blank line) 6-19. switch on operatorSymbol { ... } — the same routing pattern as the lesson above: one when per operator, only the matching one runs. 20. (blank line) 21. System.debug(result); — logs the answer.

This is the whole calculation as it stands after this lesson — plain variables, one switch statement, four operations. The next lesson adds safety around bad input.

Exercise

Add a fifth case to the switch statement above for the '%' operator (modulo/remainder), using Math.mod(a, b) — note Math.mod requires Integer arguments, so this exercise treats a and b as whole numbers.

Show hint

when '%' { result = Math.mod(a.intValue(), b.intValue()); }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Build the Operations Quiz

1. What does the switch statement in this lesson do?

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

This lesson writes the actual arithmetic — plain variables and a switch statement (Module 6) that routes to the correct calculation based on the operator.