![]() |
|
else if/switch - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: else if/switch (/Thread-else-if-switch) |
else if/switch - NmZero - 07-26-2014 Hello, what would be the deciding factor when you are considering to use else/if or switch? I don't have an example, I was jw. NmZero RE: else if/switch - lady_godiva - 07-26-2014 You might hear people talking about performance, code being more readable but no one of these would be a significant factor when talking about if vs switch. The facts: switch is believed to be faster and more readable, however with if/else you can put conditions that you expect will be executed more often on top, while the others to the bottom. In the end however it's just a matter of what you like most in my opinion. There is no significat advantage in using one or another. RE: else if/switch - NmZero - 07-26-2014 Ok, then it seems I was right after all. I heard about the performance thing, but wasn't sure if it was true, ty
RE: else if/switch - tfxla - 09-10-2014 You should use the one you like. RE: else if/switch - tfxla - 09-10-2014 You should use the one you like. RE: else if/switch - testardube - 09-10-2014 The compiler can create a jump table for certain types of switch statements which is more efficient than just evaluating each element like a nested set of if statements. so switch can be more efficient. RE: else if/switch - testardube - 09-10-2014 The compiler can create a jump table for certain types of switch statements which is more efficient than just evaluating each element like a nested set of if statements. so switch can be more efficient. RE: else if/switch - Deque - 09-11-2014 Performance shouldn't be your primary concern for the first decision of how to write your code. You should never do performance tweaks before there is a reason to, meaning, you first write your program in a well-readable manner, afterwards you check for bottlenecks with a profiler. Only then you do performance tweaks. Otherwise you end up tweaking code that does not even run often enough to be important for the performance of your program. And more importantly: Performance tweaks might introduce bugs, because very often they end up in less-readable code. People who care about the performance of every switch-case/if-else statement in their code, should rather switch to writing assembly. The deciding factor is for me readability and complexity. If-else statements allow more complex conditional statements, some of them can not be realized with a switch-case, so the decision is clear for these. Switch-case statements tend to be more readable. Just an example in Java. This is the if-else latter: Code: if(key == COFFHeaderKey.CHARACTERISTICS) {
b.append(NL + description + ": " + NL);
b.append(IOUtil.getCharacteristics(value, "characteristics"));
} else if (key == COFFHeaderKey.TIME_DATE) {
b.append(description + ": ");
b.append(convertToDate(value));
} else if (key == COFFHeaderKey.MACHINE) {
b.append(description + ": ");
b.append(getMachineTypeString((int) value));
} else {
b.append(field.toString());
}And this is it after refactoring to use a switch-case statement: Code: switch (key) {
case CHARACTERISTICS:
b.append(NL + description + ": " + NL);
b.append(IOUtil.getCharacteristics(value, "characteristics"));
break;
case TIME_DATE:
b.append(description + ": ");
b.append(convertToDate(value));
break;
case MACHINE:
b.append(description + ": ");
b.append(getMachineTypeString((int) value));
break;
default:
b.append(field.toString());
}RE: else if/switch - Banadmin - 06-11-2015 This is not down to preference... This is down to when you should use one over the other. Below I will explain. Switch statements - you can only evaluate 1 piece of information at a time If/else - you can evaluate multiple values which could potentially give you a different result each time Example: Code: switch(key)
{
case "test":
System.out.println("Case 1");
break;
case "next string":
System.out.println("Case 2");
break;
default:
System.out.println("Case 1");
break;
}Code: if(key.equals("huehue") && anotherKey.equals("some other string")
{
System.out.println("1st condition is true.");
}
elseif(key.equals("lolol") && anotherKey.equals("some other string....")
{
System.out.println("2nd condition is true.");
}RE: else if/switch - Coca. - 09-09-2015 Use "switch" when you want to possibly perform multiple tasks if a variable's value changes in your code. "Switch" can be faster in some cases, but using "else if" could be just about the same speed. "else if" and "switch" both have their pros and cons. Ultimately, it is up to you to decide when you want to use them. |