committed by
Christopher McCormack
parent
ed542ea7d4
commit
c731321661
@ -272,3 +272,5 @@ int main() {
|
||||
* Speed: A switch statement might prove to be faster than ifs provided number of cases are good. If there are only few cases, it might not effect the speed in any case. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.
|
||||
* If a switch contains more than five items, it’s implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every previous condition first.
|
||||
* Clarity in readability: A switch looks much cleaner when you have to combine cases. Ifs are quite vulnerable to errors too. Missing an else statement can land you up in havoc. Adding/removing labels is also easier with a switch and makes your code significantly easier to change and maintain.
|
||||
* Default case: In a switch statement, the requirement of a default case is necessary otherwise the program will crash for a non label match category. This situation doesn't exist in if statements as the else part is considered as the default case handler
|
||||
* Break statement: After each label, a break keyword is required due to lack of namespace distinction.
|
||||
|
Reference in New Issue
Block a user