Case 0 vs Default for segmented controls


#1

A question regarding the formatting of the code for silver challenge.

This is the addition I added to make the Segmented control buttons work.
whereAmIViewController.m

- (IBAction)setMapType:(id)sender { switch (((UISegmentedControl *)sender).selectedSegmentIndex) { case 0: { worldView.mapType = MKMapTypeStandard; break; } case 1: { worldView.mapType = MKMapTypeSatellite; break; } default: { worldView.mapType = MKMapTypeHybrid; break; } } }

My question is, how come when the application is run, it shows “case 0” first as in the standard map type instead of the “default”. Or does Default have another meaning in this case? All three buttons works as should.


#2

I don’t quite understand the question, but the default statement executes when the value of the selectedSegmentIndex is neither 0 nor 1.


#3

Sorry about my question being confusing and Thank you for answering my question (even though it was poorly phrased)!

I browsed through sample code to come to the conclusion of using this for this challenge.

My question is what’s the value of using “default” vs simply using another “case” e.g. case 2.
For instance, I replaced Default with case 2 and from what I could tell, nothing changed.


#4

Okay. I got you now.

The default: in a switch statement is used to handle all the other cases that have not been listed explicitly.

For example:

//  main.m

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    void TestRandomNumbersForSmallPrimeness (unsigned long);
    TestRandomNumbersForSmallPrimeness (32);
    return 0;
}

void TestRandomNumbersForSmallPrimeness (unsigned long count)
{
    while (count--) {
        long number = arc4random_uniform (10);
        switch (number) {
            default:
                printf ("number %lu is not a small prime!\n", number);
                break;
            case 2:
            case 3:
            case 5:
            case 7:
                printf ("number %lu is a small prime\n", number);
        }
    }
}

In the above example when the number is not a small prime, the program executes the printf statement under the default:.


#5

I see, thanks for the explanation. That makes a lot of sense and is quite clever!