C
C#7mo ago
h3ll

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
reflectronic
reflectronic7mo ago
you use one or the other when it makes the code look better, usually
RXPHXEL
RXPHXEL7mo ago
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
Angius
Angius7mo ago
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
Jimmacle
Jimmacle7mo ago
in addition, switches only support constants and pattern matching so depending on the conditions you're testing if statements might be less awkward
Mayor McCheese
Mayor McCheese7mo ago
As an aside the test case for switches in c# is something like 19k cases.
Want results from more Discord servers?
Add your server