29.4k views
1 vote
Explain the process of creating a switch statement to replace an if-then-else statement in Java.

User HVenom
by
5.0k points

2 Answers

1 vote

Answer:

The answer to this question is given in the explanation section.

Step-by-step explanation:

Let look at an if the else statement

if (condition1) {

//some code if condition 1 is true

} else if (condition2) {

// some code if condition 2 is true

} else {

// some code if condition 3 is true

}

No let look at switch statement

switch(expression) {

case x:

// code block

break;

case y:

// code block

break;

default:

// code block

Now let look at your answer.

if then else should be replaced with switch if conditions are fixed.

in the process of replacing

write your condition of if statement in the case area of swatch

User Subasri Sridhar
by
4.6k points
6 votes

Answer:

The answer to this question is given in the explanation section.

Step-by-step explanation:

Let look at an if the else statement

if (condition1) {

//some code if condition 1 is true

} else if (condition2) {

// some code if condition 2 is true

} else {

// some code if condition 3 is true

}

No let look at switch statement

switch(expression) {

case x:

// code block

break;

case y:

// code block

break;

default:

// code block

Now let look at your answer.

if then else should be replaced with switch if conditions are fixed.

in the process of replacing

write your condition of if statement in the case area of swatch

User Krimo
by
4.9k points