Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

I have another question that I need help with from Conditionals in iOS Development with Objective-C

Complete the switch statement below:

int i = 0;

_________________( i ) {

____________________ 0 :

break;

}

1 Answer

int i = 0;     //declare int variable
switch(i)    //perform switch on int variable
case 0:
          //do something
          break;      //stop searching through the switch cases

case 1:
         //do something
         break;

case 2:
        //do something, you get the idea by now, hopefully
        break;

default:
       //default case (if we didn't hit a break statement by now)
      break;      //not really needed, but still, I think it makes it cleaner to read

The switch is like a series of if statements, with the cases being the value that you're comparing against. The break statements stops the comparisons from continuing (if you don't break, execution will continue until the next break statement, regardless of whether or not your code matches the following case statements (it won't)).

The default case is for all values that were not handled in another case statement, you should always include this, even if you do nothing but break (it makes it more clear that this "doing nothing" action is intentional).

The variable in parenthesis after the switch(here) is the variable on which you're switching (e.g. an int, or a char).

I hope this helped.