Java ProgramsControl FlowSimple Calculator Using Switch

Simple Calculator Using Switch in Java

beginner·  Control Flow  ·  Switch Statement

Problem

A switch statement can branch on a single character just as easily as on an integer, making it a natural fit for dispatching on an arithmetic operator like +, -, *, or /.

Given two numbers and an operator symbol, compute the result of applying that operator.

Input
a = 12.0, b = 4.0, operator = '*'
Output
Result: 48.0

Java Program

Java
public class SimpleCalculatorSwitch { public static void main(String[] args) { double a = 12.0, b = 4.0; char operator = '*'; double result; switch (operator) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; default: throw new IllegalArgumentException("Unknown operator: " + operator); // no break needed — throw exits immediately } System.out.println("Result: " + result); } }

Output

Result: 48.0

Core Logic

Matching the operator character against each case label picks the right arithmetic operation without a chain of if/else comparisons.

How It Works
  1. 1switch (operator) compares the char against each case label in turn.
  2. 2Each of '+', '-', '*', '/' computes its own result and breaks immediately after.
  3. 3The default case throws an exception for any operator that isn't one of the four recognized symbols.
For operator = '*', the third case matches and computes 12.0 * 4.0 = 48.0.
💡

Key Point: Every case ends with break — without it, execution would fall through into the next case and silently compute the wrong operation.

Key Concepts

switch statementchar case labelsdefault case

Approach 2: Java 8

Java
import java.util.Map; import java.util.function.DoubleBinaryOperator; public class SimpleCalculatorMapLookup { public static void main(String[] args) { double a = 12.0, b = 4.0; char operator = '*'; Map<Character, DoubleBinaryOperator> operations = Map.of( // pairs each operator with its lambda '+', (x, y) -> x + y, '-', (x, y) -> x - y, '*', (x, y) -> x * y, '/', (x, y) -> x / y ); double result = operations.get(operator).applyAsDouble(a, b); System.out.println("Result: " + result); } }

Output

Result: 48.0

Core Logic

A map from operator to operation replaces the switch entirely — looking up the right function and calling it does the same dispatch in one line.

How It Works
  1. 1Map.of(...) builds an immutable map pairing each operator character with a DoubleBinaryOperator lambda.
  2. 2operations.get(operator) retrieves the lambda matching the given operator.
  3. 3.applyAsDouble(a, b) calls that lambda with both operands, producing the result directly.
For operator = '*', the map returns the multiplication lambda, and calling it with 12.0, 4.0 gives 48.0.
💡

Key Point: Adding a new operator here means adding one more map entry, not another case block — the dispatch logic itself never changes.

Key Concepts

Map.of()DoubleBinaryOperatorlambda expression

Related Programs