how to combine switch and if else statements
-
30-09-2019 - |
Question
I'm taking an online java class and the teacher has asked for the following: Write a menu program that ask a user for a number indicating the four basic math equations(addition, subtraction, multiplication, division). Using a if/else structure to do the required operation, ask the user for inputs and solve the equation. I am new to this, but I don't see the point of the if/else, but I guess that is irrelavent.
If I use case 1, 2, 3, 4 as below to determine the operation, how do I refference the case with a 'If statement'?
int userSelection;
double x;
double y;
double answer;
switch ( userSelection ) {
case 1:
TextIO.putln("Let's add! ");
TextIO.putln();
TextIO.putln("Enter the first number: ");
x = TextIO.getlnDouble();
TextIO.putln("Enter the second number: ");
y = TextIO.getlnDouble();
break;
//I thought I could use the 'If' like this
if (case 1); // I don't know how to refference this correctly
answer = x + y;
TextIO.putln( x 'plus' y 'equals' answer);
Thanks in advance!
Solution
It would appear to me that your instructor does not want you to use the switch
statement to begin with, but simply use the if
flow control structure:
if(booleanExpression)
{
}
else if(booleanExpression2)
{
}
OTHER TIPS
The switch
is used against a single given variable/expression and then cases are used on what value it is but because you are not testing a single variable/expression, this is not applicable, you should use if-elseif-else
structure instead.
The switch case statements is a structural alternative to using nested if..else statements where you want to run differnet logic for different values of a numerical or enumerical data type. It does not combine with if..else and the 2 are different approaches to solve conditional problems. Which one is better suited depends on the condition you are evaluating.
If I understand your code correctly, then I think you are looking for something like this:
switch ( userSelection ) {
case 1:
TextIO.putln("Let's add! ");
TextIO.putln();
TextIO.putln("Enter the first number: ");
x = TextIO.getlnDouble();
TextIO.putln("Enter the second number: ");
y = TextIO.getlnDouble();
answer = x + y;
TextIO.putln( x + " plus " + y + " equals " + answer);
break;
case 2:
... //oh, I don't know. but I would guess: let's subtract!
break;
case 3:
... //let's multiply!
break;
case 4:
... //let's divide!
break;
}
Here's the presudo code for a rewrite of a CASE
statement:
IF (Case==1) THEN
*What to do when Case==1.*
ELSE IF (Case==2) THEN
*What to do when Case==2.*
ELSE IF (Case==3) THEN
*What to do when Case==3.*
ELSE IF (Case==4) THEN
*What to do when Case==4.*
END IF
You'll have to code this in Java though. :) Good luck with your assignment.
You're switching on userSelection so userSelection already has the value you need; you can reference the value by referencing the userSelection variable:
if (userSelection == 1) {
// add
} else if (userSelection == 2) {
// subtract
} else if (userSelection == 3) {
// multiply
} else if (userSelection == 2) {
// divide
}