GreenSalamanderCS's post
A Comparison of If-Else & Switch Statements
Syntax
An if-else statement uses if & else keywords, while a switch statement uses keyword & case labels.
Conditionals
An if-else statement can handle any type of condition while a switch statement can only handle equality conditions.
Execution
An if-else statement uses top-down logic. It runs through each condition, one-by-one, going in order until it reaches the end of the statement or until one is true. A switch statement jumps directly to the matching case.
Default Case
If no conditions are met, the optional default case is executed in the form of an 'else' block for an if-else statement. However, for a switch statement, the declaration of a default case is required if no cases match.
Readability
Depending on the complexity of the conditions inside the if-else statement, it can become difficult to read. A switch statement is very straightforward, as there are many cases, making its syntax more concise & easier to read.