when to use switches and when to use if statements?
do i just use switches if i need more than one if statement?
and what is using switches instead of if statements exactly efficient in?
5 Replies
you use one or the other when it makes the code look better, usually
As a beginner, I use switch cases when there's 4 or more statemets, if I have less than 4 I go with if else statements
When the IDE tells me to
In general, though, switch is usually used when switching on some particular value. Like a test score, for example
Each branch just checks the value of that variable
If/else if/else chains are used when each condition is different, or uses different variables
in addition, switches only support constants and pattern matching so depending on the conditions you're testing if statements might be less awkward
As an aside the test case for switches in c# is something like 19k cases.