Menu
CoddyTech

Recap - If Else

Part of the Fundamentals section of Coddy's Java journey. Lesson 31 of 73.

challenge icon

Challenge

Beginner

You are given a code which gets as input two numbers n1 and n2 and a single char string op.

Note: we will learn in next lessons how to get input from the user. For now, the input lines above your code are already written.

 

The possible values for op are +, -, / and *

Your task is to set the variable result based on the conditions:

  • if op is +, set result with n1 + n2.
  • if op is -, set result with n1 - n2.
  • if op is /, set result with n1 / n2.
  • if op is *, set result with n1 * n2.

 

Important: In Java, strings cannot be compared with ==. To compare a string, use the .equals() method instead. For example, to check if op is +, write: op.equals("+")

Try it yourself

        // Write your code below
        double result = 0;
        
        
        

All lessons in Fundamentals

Practice on your own: Online Java compiler