Reputation: 17160
I have always wondered but can't find a definitive answer. When should I use either an 'if' or 'switch' statement. When is one better than the other?
For example I could do:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//Stuff
break;
case 1:
//Stuff
break;
default:
break;
}
//OR
if (buttonIndex == 0) {
//Stuff
}
if (buttonIndex == 1) {
//Stuff
}
}
This is just an example, but I'm sure in different situations to this it would matter differently.
Upvotes: 5
Views: 3807
Reputation: 49
If you are only checking if one variable is of a certain value (buttonIndex == 1) and doing this for several values you'd be better of using a switch
.
However if you are using some more complex conditions (maybe even including functions in the condition), well then you should use if statements
.
Upvotes: 4
Reputation: 1480
Switch
just like if elseif
. Too many Switch
or if elseif
will make your code very difficult to read and understand. But no any function issue.
Upvotes: 0
Reputation:
I'd like to see code as near to human understanding as possible. That's why three forms of decisions (if-then-else, switch-case-default, ?:) are suited to map three different though processes:
if
)switch
?:
So use the one which maps to the way you think of the actual decision as a human.
Of course, it should be stressed that proper OO solution in lots of scenarios of ... and we do ... which we do this way for ..., this way for ... and this way for ..., where often switch
is used (but it holds for some if
s as well) is to not use these constructs and replace them with polymophism. Manual decision making should only be done when there is a real decision, not to fake (logical) polymorphism.
Upvotes: 3
Reputation: 515
If 'if' and 'switch' are so evil they should have been removed from modern programming languages. The fact is they are still there. I think the key is we should try our best to make our code works correctly and efficiently, easy to understand and maintain. OOP is just one way to write programs, we shouldn't write programs for pure OOP. So it's really a subjective problem.
Upvotes: -3
Reputation: 31559
In this case this doesn't matter. The reason switch exists is that it's faster than if. If you write a long code of if ... else if ... else if ...
it will perform slower than switch because if
does iterative search (O(n)) while switch
does table based lookup (O(1))
Upvotes: 13
Reputation: 4079
it depends on what you want to do inside it. keep in mind you cant alloc new object inside switch statement
Upvotes: -1
Reputation: 89509
I believe this is a subjective question. The answer is really up to you.
For my own code, I like using switch
statements as it's pretty clear that what is happening is the result of some condition that can do other things if the condition is different.
But if the code underneath a switch
case:
statement is rather lengthly or convoluted, then for my own code I do the second thing: the if (buttonIndex == 0) {
bit.
Upvotes: 7