Login Register






else if/switch filter_list
Author
Message
else if/switch #1
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

Reply

RE: else if/switch #2
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.
Everything is relative

Reply

RE: else if/switch #3
Ok, then it seems I was right after all.
I heard about the performance thing, but wasn't sure if it was true, ty Smile

Reply

RE: else if/switch #4
You should use the one you like.

Reply

RE: else if/switch #5
You should use the one you like.

Reply

RE: else if/switch #6
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.

Reply

RE: else if/switch #7
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.

Reply

RE: else if/switch #8
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()); }
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: else if/switch #9
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."); }

Reply

RE: else if/switch #10
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.

Reply