Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 798
mbm29414 Avatar asked Aug 21 '14 13:08

mbm29414


2 Answers

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.

like image 63
James Snook Avatar answered Oct 29 '22 09:10

James Snook


Reading your ideas about switch and return, yes, a return statement can be considered as a specialized break statement.

A break is just a branch table abstraction, so as return is, so at the end of the day, they will simply execute a jump instruction.

like image 39
henrique Avatar answered Oct 29 '22 10:10

henrique