"break;" in Obj-C

Discussion in 'Mac Programming' started by sheepopo39, Dec 6, 2009.

  1. sheepopo39 macrumors 6502

    Joined:
    Sep 18, 2008
    #1
    I've been working through Cocoa Programming for Mac OS X for dummies and I'm at one point where you have to work with a pop up menu, and part of the code is...

    Code:
    switch (operation) {
    
    case 0:
    answer = num1 + num2;
    break;
    
    case 1:
    answer = num1 - num2;
    break;
    And so on

    My question is, what exactly does the "break;" do? I'm stumped with this, I couldn't find it in the documentation.

    Also, what exactly is the "switch" and "case" for.
     
  2. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #2
    The switch statement is used in place of lots of "else if's." Your example could be re-written as follows using else-if's with a couple extra else-if's to make the point more clear:

    Code:
    if (operation == 0)
    {
         answer = num1 + num2;
    }
    else if (operation == 1)
    {
         answer = num1 - num2;
    }
    else if (SOME OTHER CONDITION HERE)
    {
         SOME OTHER STATEMENT HERE;
    }
    else (LAST CONDITION HERE)
    {
         SOME DEFAULT STATEMENT HERE;
    }
    I think of the Switch as sort of like ordering from a takeout menu, basically you get some statement at the top, and then you go down the list of cases until you find the one that matches, execute the code and then you "break" out of the whole thing and are on to your next line of code outside of the entire switch block.

    Some people hate using Switches, but they look cleaner to me than lots of else-if statements.
     
  3. Kaliemon macrumors member

    Joined:
    Apr 30, 2006
    #3
    GorillaPaws beat me to it, but I will leave this up since I typed it up.

    First the switch statement can be thought of as condensing a bunch of if statements into one.

    Example:

    Code:
    if(operation == 0)
    answer = num1 +num2
    else if(operation ==1)
    answer = num1 - num2
    The break tells the switch "I did what needs to be done, exit the switch and continue"

    Sometimes you may want the switch to fall through (2 or more cases do the same thing or one case may continue)

    Code:
    switch(operation){
    
    //This case doesn't fall through
    case 0:
    answer = num1 + num2;
    break;
    
    //This case does
    case 1:
    case 2:
    answer = num1 - num2;
    break;
    
    //This case does its own thing and falls through
    case 3:
    answer = num1 * num2;
    case4:
    answer = num1 / num1;
    break;
    }
    Hope that all helped
     
  4. carlos700 macrumors regular

    carlos700

    Joined:
    Dec 17, 2004
    Location:
    Omaha, NE
    #4
    A switch statement executes code depending on the variable in question. So if operation contains 0, then
    Code:
    answer = num1 + num2
    will execute.
    Code:
    break;
    tells the switch statement to exit. Without it, the switch statement would continue executing all the code following the original case it started in.
     
  5. sheepopo39 thread starter macrumors 6502

    Joined:
    Sep 18, 2008
    #5
    Ohhhh ok, I get it, thanks a lot everyone. So then the "Switch" and "Case" sort of act together like an else if statement.
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    And I feel compelled to add that break is also used to force an exit from loops.

    Code:
    for (;;)
    {
        if ( true ) break;          // exit forever-loop
    }
    
    while ( true )
    {
        if ( true ) break;          // exit forever-loop
    }
    
    do
    {
        if ( true ) break;          // exit forever-loop
    } while ( true )
    
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    It is worth noting that forgetting the "break" is a common source of bugs. The situation where a fall-through is desired is much rarer than the "break", so in retrospect it would have been better to break by default and re-use the "continue" keyword for explicit fall-through.

    It was probably easier to implement a default fall-through (because the compiler can generate simple jump tables).
     
  8. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #8
    It's good practice that whenever you intend to fall through to the next switch statement, you put in a comment stating that it's intentional. Otherwise, the safe assumption is that a missing "break" inside a "case" is a bug.

    I will dispute the claim that it's much rarer to fall through. It's really common to fall through when you want multiple case results to do the exact same thing:

    Code:
    switch(dayOfWeek)
    {
    case SATURDAY:
    case SUNDAY:
        bIsWeekend = true;
        bEndsInY = true;
        break;
    
    case MONDAY:
        bHaveACaseOfTheMondays = true;
        // fall through
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        bIsWeekend = false;
        bEndsInY = true;
        break;
    
    default:
        bEndsInY = false;
        break;
    }
     

Share This Page