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
Decimal a = 12;— the first operand.Decimal b = 5;— the second operand.String operatorSymbol = '+';— which operation to perform.Decimal result;— declared, not yet assigned — will hold whichever calculation runs below.- (blank line)
switch on operatorSymbol {— this is exactly theswitch onpattern from Module 6:operatorSymbolis checked once, and the matching branch below runs. 7-9.when '+' { result = a + b; }— matches here, sinceoperatorSymbolis'+'. 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.}— closes theswitchblock.- (blank line)
System.debug(result);— logs17, from12 + 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
Decimal a = 12;— first operand.Decimal b = 5;— second operand.Decimal sum;— will hold the addition result.Decimal product;— will hold the multiplication result.- (blank line)
switch on 'add' {— checking the literal text'add'directly this time, instead of a variable.when 'add' {— matches.sum = a + b;— runs.}— closes thewhen.}— closes theswitch.System.debug(sum);— logs17.- (blank line)
switch on 'multiply' {— a second, separate switch.when 'multiply' {— matches.product = a * b;— runs.}— closes thewhen.}— closes theswitch.System.debug(product);— logs60.
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()); }
Build the Operations Quiz
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.