From c73132166153965cd4e3eec0b606b7d8cd7998c0 Mon Sep 17 00:00:00 2001 From: Kaustubh J Date: Tue, 29 Jan 2019 23:49:49 +0530 Subject: [PATCH] Update index.md (#32520) Added few more points for Switch vs If-else --- guide/english/c/switch/index.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/guide/english/c/switch/index.md b/guide/english/c/switch/index.md index 2b99ef18dd..715d72adda 100644 --- a/guide/english/c/switch/index.md +++ b/guide/english/c/switch/index.md @@ -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.