Search code examples
objective-creturnswitch-statement

Does a "return..." statement make a "break;" moot?


I came to Objective-C from a VB.NET background where the switch statement is Select Case and no break statements are needed (or possible).

I know that the general rule is to place a break statement at the end of each case statement so that execution doesn't "fall through" to the next case statement.

When writing iOS apps, I frequently have switch statements in my -tableView: heightForRowAtIndexPath: methods. Basically, I often let my cells report the height needed, so I end up with switch statements like this:

switch (indexPath.row) {
    case 0:
        return ...
        break;
    case 1:
        return ...
        break;

    ...

    default:
        return ...
        break;
}

I saw this answer, which makes sense to me and is what I expect the answer to be, but that question is specifically about Java, and I wanted to see if the same answer holds true for Objective-C.

I also found this answer, which relates to C, which I assume is the correct answer for Objective-C, as well.

So, is a return statement a specialized break statement?


Solution

  • No a return statement is not a specialised break. return causes you to exit the function break causes you to exit the switch statement. You don't need the break if you have the return, but they are different things.