Another problem with switch is that you have to remember to provide the default: case otherwise you aren't covering all the cases! Its the same thing that happens when you forget the else block as shown in your if-else example. Its possible to leave out the default case in either scenario.
The break is another annoying thing and not just C/C++, even in Java and C#, you need to provide the break but I don't remember since its a long while since I've coded in those languages.
Python lacks a switch statement but I've never felt the need for it frankly. Ultimately, its just a more fancy variety of if statement!
Well, if you try to use a c-style switch statement that's definitely a problem. That's why I suggest you to look into the Rust way of switch: matching. It has all the good parts of switch but not the bad ones.
Another problem with switch is that you have to remember to provide the
default:
case otherwise you aren't covering all the cases! Its the same thing that happens when you forget theelse
block as shown in your if-else example. Its possible to leave out the default case in either scenario.The
break
is another annoying thing and not just C/C++, even in Java and C#, you need to provide the break but I don't remember since its a long while since I've coded in those languages.Python lacks a switch statement but I've never felt the need for it frankly. Ultimately, its just a more fancy variety of if statement!
Well, if you try to use a c-style switch statement that's definitely a problem. That's why I suggest you to look into the Rust way of switch: matching. It has all the good parts of switch but not the bad ones.
C++ compilers will tell you when you've forgotten a
default
block. I didn't remember C# needing it, since it can't fall through to the next block.