#Switch and If then are the same?

1 messages · Page 1 of 1 (latest)

formal sand
#

if not then what are the differences?

dusty flowerBOT
#

<@&987246399047479336> please have a look, thanks.

dusty flowerBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

wind sage
#

first of all, the syntax and intention is different

#

second, switch is implemented more efficient, as its a jump table

#

its minor though, maybe a few nanoseconds

unique aspen
#

The switch statement is used in different situations than the if statement
You can replace a switch for a number of if statements, but that would make no sense.
The switch is mostly used to test 1 thing against multiple conditions.

String userInput = "";
switch(userInput){
  case "hi": // If userInput is "hi"
    System.out.println("hello!");
    break;
  case "are you a robot": // If userInput is "are you a robot"
    System.out.println("no");
    break;
  case "where can I learn Java": // If userInput is "where can I learn Java"
    System.out.println("In the Together Java Discord server!");
    break;
  default: // If userInput is something else
    System.out.println("I have no idea what you just said");
    break;
}

You could write this with a few if statements, but using a switch makes it clear what intention you have (I also think it looks cooler)

#

This is a silly example and it could be written even better but that's not the point

wind sage
#

note that we have switch-expressions since a couple of years. nice stuff

unique aspen
#

It looks awesome imo, especially the enhanced one
You could write the above like this:

String output = switch (userInput) {
    case "hi" -> "hello";
    case "are you a robot" -> "no";
    case "where can I learn Java" -> "In the Together Java Discord server!";
    default -> "I have no idea what you just said";
};
System.out.println(output);
paper oracle
#

if it gets really big a Map<String, String> makes it more readable

#

for example language files

stark tusk
paper oracle
#

wouldnt you still load language mapping files into Map<String, String>?

stark tusk
spring agate
#

something that hasnt been brought up yet is if/else can create a range that switch cant.

if (0 <= age && age <= 13) { "kid" }
else if (14 <= age && age <= 17) { "teen"}
else if (18 <= age) { "adult" }
else {exception}```
In this example its discrete numbers [(1,2,3,...13) e.g.] but it also works for continuous stuff like tax brackets
formal sand
#

Thank you guys I see

#

very clear!!