Switch Statement Versus If Statements

Hi I’m a huge noob with programming and have a simple question.

Is there a performance or maybe coding time advantage using a Switch statement versus lots of If statements? Or is it just personal preference?

-Thanks!!!

Switch statements are cleaner if you would have many many if statements otherwise. They are also useful if you want multiple values to run the same code.

Instead of:


int value = derp();

if(value == 2 || value == 5) {
    doThis();
}
else if(value == 3 || value == 7) {
    doThat();
}
else if(value == 1 || value == 0) {
    doThose();
}
else if(value < 0 || value >= 8 || value == 6 || value == 4) {
    DoThese();
}

This would simply go down to:


switch(derp()) {
    case 0: case 1:          doThose(); break;
    case 2: case 5:          doThis(); break;
    case 3: case 7:          doThat(); break;
    case 4: case 6: default: doThese(); break;
}

EDIT: @Riven, the “default” keyword doesn’t highlight :S

I like If’s when you need to meet previous conditions and would like to handle what happens in the middle:

public void test(boolean a,boolean b,boolean c){
		if (a){
			statement;
			if(b){
				statement;
				if(c){
					statement;
				}else{
					statement;
				}
			}else{
				statement;
			}
		}else{
			statement;
		}
	}

Also, in really complicated algorithms you could use both:

public void test(int a,boolean b,boolean c){
		switch(a) {
	    case 0: if(b && c){ statements } else { statements } break;
	    case 1: if(b && !c){ statements } else { statements } break;
	    case 2: if(!b && !c){ statements } else { statements } break;
		}
	}

As far as I know, there are no performance gain or loss between either each of them, just use the one that fits best your situation and makes your code clearer.

BTW, off topic, I really hate the “break” command, so I try not to use switch :frowning: it has something to do with “go-to” in my head, so I hate it >:(

tl;dr
Both are just fine, they are tools to control the flow of the program, just use the one that fits you best :slight_smile:

IMO If is more general than Switch in term of use. If can take more conditional syntax.

There is absolutely no reason to hate the ‘break’ command, mostly because you can’t live without, also because it is nothing like goto.

Wow this was really thorough information, thanks for the feedback everyone.

switch uses optimised code to select values and jump to the appropriate code. A switch on a reasonable number of values will be somewhat faster than the equivalent code formed from if-elseifs.

Cas :slight_smile:

Switch/case is great with enums. For strings (1.7 and up) it switches on the string’s hashCode, which should be a great deal faster than testing them in series. The important part is not which is faster, it’s which is appropriate.

I’m torn as to whether that’s a ‘good thing’ ™.

I’m sure there are a few useful situations where it’s perfectly reasonable to switch on Strings, and doing so will no doubt avoid some boiler plate.

However I’ve a feeling it’ll be misused to create bad, or poorly structured code. Using Strings where enums would be a better fit for instance.

User input.

How does that change anything?

I’m giving a scenario where strings in switch is helpful.

I use 1.7 and never ever use that string switch until now.

You’re still hardcoding string literals in your code, so Enums are still prefered, due to type safety (unlikely you’re doing to write those strings only once).

I’m not big on the idea of string switches either, and I never use them, but since strings are hashable and immutable, I don’t see much reason to not support them in switches. It can come in handy for simple command line parsing, for example, since constructing a hashtable is a lot of boilerplate. It’s certainly not the most impressive feature of 1.7 though.

Except on enums, where you actually get warned about unhandled cases, switch in general can tend to lead to sloppy code. Still useful sometimes.

yeah one of my use cases of string switches are when I do file parsing