Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sheepopo39

macrumors 6502
Original poster
Sep 18, 2008
251
0
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.
 
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.
 
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
 
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.
 
Ohhhh ok, I get it, thanks a lot everyone. So then the "Switch" and "Case" sort of act together like an else if statement.
 
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 )
 
Sometimes you may want the switch to fall through (2 or more cases do the same thing or one case may continue)

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).
 
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).

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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.