477.Build a Simple Four-Operation Calculator
Easy
Given two integers
a and b and an operator op — one of "+", "-", "*", or "/" — perform that operation on a and b and return the result.
The if-else chainIf-Else ChainA sequence of if / else-if / else blocks, tried in order until one condition matches. version tests each operator string one at a time. The switch statementSwitch StatementA multi-way branch on a single value's exact matches — jumps directly to the matching case instead of testing conditions sequentially. version expresses the exact same four-way choice more directly, since every branch is an exact match against one fixed value rather than a general condition.
Example 1:
Input: a = 6, b = 3, op = "+"
Output: 9
Example 2:
Input: a = 10, b = 4, op = "-"
Output: 6
Example 3:
Input: a = 5, b = 6, op = "*"
Output: 30
Example 4:
Input: a = 20, b = 4, op = "/"
Output: 5
+ 5 hidden test cases run on Submit.
Constraints:
- ●
-1000 ≤ a ≤ 1000 - ●
-1000 ≤ b ≤ 1000 - ●
op is one of "+", "-", "*", "/" - ●
b ≠ 0 whenever op is "/"
a =
6
b =
3
op =
+