DEV Community

loading...

Discussion on: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else`

Collapse
theringleman profile image
Sam Ringleman

Considering it rapidly speeds up your conditional statements, I will always use a switch statement. A switch statement acts as though you are accessing an array via an index. Whereas an elseif statement has to process each conditional statement until it passes.

Collapse
andrewharpin profile image
Andrew Harpin

Depends on if scripted Vs compiled.

A compiler will optimise into either the conditions or a jump table depending on the code. Usually for both switch and if else

Collapse
theringleman profile image
Sam Ringleman

As someone who has never had the chance to jump into a compiled language, this is wonderful information! Thank you for the correction.

Collapse
theringleman profile image
Sam Ringleman

Of course this depends on the context of the application though. If I am checking against static values, then my previous statement is true.