Josh Kahane
Josh Kahane

Reputation: 17160

Should I use an if or switch Statement? Objective-C

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

Answers (8)

chrs
chrs

Reputation: 6096

Switch statements are faster the nested if statements. That's it.

Upvotes: 1

leverse
leverse

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

Tinyfool
Tinyfool

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

user1046334
user1046334

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:

  • ... and now, in an event there is a frobnicated quibplatz, we should do ... (otherwise ... should be done) is a mapping for if )
  • ... and now based on the quibplatz value, we should do ... if it is 0, ... if it is 1 (, ... if it is none of them) if a mapping for switch
  • ... we use ... or ... based on the fact if ... holds or not is mapping for ?:

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 ifs 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

Bruce Li
Bruce Li

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

Daniel
Daniel

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

orthehelper
orthehelper

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

Michael Dautermann
Michael Dautermann

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

Related Questions